Skip to content

Instantly share code, notes, and snippets.

@OwenMelbz
Created July 16, 2015 14:46
Show Gist options
  • Save OwenMelbz/5ee9cbfc3c603e71a5dc to your computer and use it in GitHub Desktop.
Save OwenMelbz/5ee9cbfc3c603e71a5dc to your computer and use it in GitHub Desktop.
Javascript Section Scrolling with Mousewheel (needs jquery, underscorejs)
var sections = $('[data-tag]:not(.navigation):not(.footer):not(.story-time)'),
activeSection = null,
getNextSection = function(){
var coords = [],
top = $(window).scrollTop();
_.each(sections, function(s){
coords.push({
top : $(s).offset().top,
el : $(s),
tag : $(s).data('tag'),
height: $(s).outerHeight()
});
});
coords = _.sortBy(coords, 'top');
possible = _.filter(coords, function(e){
return e.top > top;
});
//possible.length && console.log(possible[0].tag);
return possible.length ? possible[0] : null;
},
nextSection = getNextSection(),
handleScroll = function(event){
activeSection = nextSection;
nextSection = getNextSection();
if( nextSection ){
dir = event.originalEvent.wheelDelta || -event.originalEvent.detail;
if( dir < 0 ){
if( activeSection && activeSection.height > $(window).innerHeight() ){
bottomPos = $(window).scrollTop() + $(window).innerHeight();
if( nextSection.top < bottomPos ){
$('html, body').stop().animate({
scrollTop: nextSection.top + 1,
}, 1000, 'easeOutExpo');
return false;
}
}
else {
$('html, body').stop().animate({
scrollTop: nextSection.top + 1,
}, 1000, 'easeOutExpo');
return false;
}
}
}
};
$(document).on('mousewheel DOMMouseScroll', _.throttle(handleScroll, 500));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment