2

The header is fixed when scrolled down. However, when scrolled back up all the way to the top, the header detaches and is unfixed again. How do I maintain a prolonged fixed header?

How do I edit jQuery or CSS code, or can I download a plugin?

Here's the example I'm basing off of: http://www.chipotle.com/en-US/html/cultivate.html#fnd

6
  • Duplicate stackoverflow.com/questions/7013408/… Commented Nov 16, 2011 at 22:24
  • Thanks, couldn't find it at first but that was helpful. Commented Nov 16, 2011 at 22:30
  • Yes Lobstrosity's answer is the one I would go with. Commented Nov 16, 2011 at 22:31
  • It's not working. I want the banner to remained fixed to the top of the browser when scrolled back to the top, so that it will be blocking the banner slightly. Commented Nov 16, 2011 at 23:04
  • Answer was found here: stackoverflow.com/questions/7013408/… Commented Nov 17, 2011 at 8:13

1 Answer 1

6

The following jQuery should accomplish what you are looking for:

$(document).ready(function() { 

  $(window).scroll(function(e) {  
    if ($(window).scrollTop() > x) { 
      $('.stayStill').addClass("fixed");
    } else {
      $('.stayStill').removeClass("fixed");
    } 
  }); 

}); 

x (which should be changed to a number (i.e. 100, 120, etc)) marks how many pixels from the top you have to be when the div is set to the fixed class. Once the person goes back above x pixels, it will be set back to normal, by removing the fixed class.

Also, .stayStill would represent your navigation bar, or whatever class you want to stall still.

Your fixed class should consist of the following CSS:

position: fixed;
Sign up to request clarification or add additional context in comments.

5 Comments

It's not working. Any idea why? I changed the class to 'page-header2' which has been my class (renamed from Twitter bootstrap) and I added the CSS as well. Also changed pixels to 100.
It's not working. Any idea why? I changed the class to '.page-header2' which has been my class (renamed from Twitter bootstrap) and I added the CSS as well. Also changed pixels to 100. The header is just fixed and follows the scrolling. It doesn't attach to the window or pop back into place.
Here's a basic demo I just made; hopefully it will help. joshfoskett.com/jQuery/Scroll
Wow! I am astonished that you went through that type of length to show me an example. I cannot thank you enough for doing this. Thank you, thank you, thank you! It was totally an error on my part but I truly appreciate your help!
@Josh - nice, works well with FF, but IE7 is not cooperating :( any fix for it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.