Skip to content

Instantly share code, notes, and snippets.

@NathanQ
Last active June 2, 2016 21:17
Show Gist options
  • Save NathanQ/5521431 to your computer and use it in GitHub Desktop.
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.
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