7

I found this website today http://www.forward-thinker.co.uk/. Does anyone know how to make a sticky header like this.?

9
  • 4
    Firefox + Firebug = find out how it works Commented Aug 2, 2011 at 13:49
  • 2
    or Internet Explorer + F12 :-P Commented Aug 2, 2011 at 13:54
  • I tried this, if I disable JS then it doesn't work. If you use firebug or chrome dev tools you can't remove the JS code without refreshing the page which puts it all back. Commented Aug 2, 2011 at 13:55
  • @DaveS, you wont be able to disable JS and have it work. For graceful degradation, just leave the header at the top of the page. Commented Aug 2, 2011 at 14:08
  • Thy shalt view source! You know what, web development is screwed up, whatever you do, you have to publish your actual code and people can see it, try doing the view source. Commented Aug 2, 2011 at 14:12

1 Answer 1

8

That particular site utilizes jQuery's $(window).scroll event. It checks if the top of the scrolled-to area ($(this).scrollTop();) is below a certain height. If so, it adds to css of the header a class containing position:fixed (it also changes its height, since only part of the header scrolls). If the scrolled-to area is nearer to the top, the script removes the class containing position:fixed from the header's css and changes the height back to its original value.

Sign up to request clarification or add additional context in comments.

4 Comments

As a recommendation, I would suggest toggling a class on the header using the scroll event. Keep the CSS where it belongs in css files.
@zzzzBov Great suggestion! That's actually what the site does. Updating answer...
@NickAldwin, I'm not seeing the class change in Firebug.
@zzzzBov I see it in Chrome... The relevant part of the js can be seen here pastebin.com/pACGhtem (line 171)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.