Skip to content

Instantly share code, notes, and snippets.

@kevincennis
Last active December 16, 2015 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincennis/5413049 to your computer and use it in GitHub Desktop.
Save kevincennis/5413049 to your computer and use it in GitHub Desktop.
// 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