Skip to content

Instantly share code, notes, and snippets.

@mikedugan
Created November 7, 2013 14:40
Show Gist options
  • Select an option

  • Save mikedugan/7355654 to your computer and use it in GitHub Desktop.

Select an option

Save mikedugan/7355654 to your computer and use it in GitHub Desktop.
script to provide smooth scrolling
//User settings
var maxSections = 5;
var sectionNamePrefix = 'page-section';
var currSection = 1;
function scrollToHash(e,sectionNumber) {
var hash = '#' + sectionNamePrefix + sectionNumber;
var target = $(hash);
target = target.length ? target : $('[name=' + hash.slice(1) +']');
// e = event;
if (target.length) {
e.preventDefault();
$('html,body').stop().animate({
scrollTop: target.offset().top
}, 600, function() {
location.hash = hash;
});
}
}
$(window).on('mousewheel', function(event, delta, deltaX, deltaY) {
if (deltaY > 0) {
if (currSection > 1) { currSection--; }
scrollToHash(event,currSection);
}
else {
if (currSection < maxSections) { currSection++; }
scrollToHash(event,currSection);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment