Last active
June 2, 2016 21:17
-
-
Save NathanQ/5521431 to your computer and use it in GitHub Desktop.
jQuery window scroll fx to add or remove a class based on the div's position from top or bottom of the page.
This file contains 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 activeToggle ($theDiv) { | |
var divTop = $theDiv.offset().top - windowTop; | |
if (divTop > cutoffTop && divTop < cutoffBottom ) { | |
$theDiv.addClass('active'); | |
} | |
else { | |
$theDiv.removeClass('active'); | |
} | |
} | |
$(window).scroll( function() { | |
windowHeight = $(window).innerHeight(); | |
windowTop = $(window).scrollTop(); | |
cutoffTop = 20; // # is distance in pixels from top of page | |
cutoffBottom = windowHeight - 360; // # is distance in pixels from bottom of page | |
$('.theDiv').each( function( index ){ | |
activeToggle($(this)); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment