Skip to content

Instantly share code, notes, and snippets.

@NathanQ
Created May 5, 2013 16:42
Show Gist options
  • Save NathanQ/5521345 to your computer and use it in GitHub Desktop.
Save NathanQ/5521345 to your computer and use it in GitHub Desktop.
jQuery stick to the top of the page header
$(function() {
var schtickm = $("header"), pos = schtickm.offset();
$(window).scroll(function() {
if($(this).scrollTop() > (pos.top + 10) &&
schtickm.css('position') == 'static') { schtickm.addClass('fixed'); }
else if($(this).scrollTop() <= pos.top &&
schtickm.hasClass('fixed')){ schtickm.removeClass('fixed'); }
})
});
@NathanQ
Copy link
Author

NathanQ commented Jun 18, 2013

This assumes the css of the header has a position rule of static.

@ttran4040
Copy link

.fixed{
position: fixed;
top:0;
bottom:0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment