Skip to content

Instantly share code, notes, and snippets.

@knorthfield
Last active August 29, 2015 14:13
Show Gist options
  • Save knorthfield/eb42b8a05d5c4f78b750 to your computer and use it in GitHub Desktop.
Save knorthfield/eb42b8a05d5c4f78b750 to your computer and use it in GitHub Desktop.
Fix element when scrolled offscreen
$(function(){
var unfix_distance = $('nav#menu').offset().top,
$fixed_element = $('nav#menu');
window.onresize = function() {
unfix_distance = $('nav#menu').offset().top;
}
$(window).scroll(function(){
if( $fixed_element.offset().top - $(window).scrollTop() < 0 ){
$fixed_element.addClass('fixed');
}
if( $(window).scrollTop() < unfix_distance ){
$fixed_element.removeClass('fixed');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment