Last active
August 29, 2015 14:13
-
-
Save knorthfield/eb42b8a05d5c4f78b750 to your computer and use it in GitHub Desktop.
Fix element when scrolled offscreen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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