Last active
December 16, 2015 09:29
-
-
Save kevincennis/5413049 to your computer and use it in GitHub Desktop.
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
// populate | |
$('.sections').each(function(){ | |
var min = $(this).offset().top | |
, height = $(this).outerHeight(); | |
offsets.push({min: min, max: min + height}); | |
}); | |
// turns into this... | |
var offsets = [ | |
{min: 300, max: 600}, | |
{min: 600, max: 900}, | |
{min: 900, max: 1200} | |
]; | |
// react | |
$(window).on('scroll', function(){ | |
var scrollTop = document.body.scrollTop | |
, activeSectionIndex; | |
$.each(offsets, function(i, obj){ | |
if (scrollTop >= obj.min && scrollTop < obj.max ){ | |
activeSectionIndex = i; | |
return false; | |
} | |
}); | |
// do something | |
$('.sections').eq(activeSectionIndex).addClass('active'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment