Created
April 3, 2017 07:12
-
-
Save daltonnyx/d33857c7fd314823fbc8d137af0deb82 to your computer and use it in GitHub Desktop.
Smooth Scrolling
This file contains hidden or 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
jQuery(document).ready(function($){ | |
if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false); | |
window.onmousewheel = document.onmousewheel = wheel; | |
function wheel(event) { | |
var delta = 0; | |
if (event.wheelDelta) delta = event.wheelDelta / 120; | |
else if (event.detail) delta = -event.detail / 3; | |
handle(delta); | |
if (event.preventDefault) event.preventDefault(); | |
event.returnValue = false; | |
} | |
var goUp = true; | |
var end = null; | |
var interval = null; | |
function handle(delta) { | |
var animationInterval = 20; //lower is faster | |
var scrollSpeed = 20; //lower is faster | |
if (end == null) { | |
end = $(window).scrollTop(); | |
} | |
end -= 20 * delta; | |
goUp = delta > 0; | |
if (interval == null) { | |
interval = setInterval(function () { | |
var scrollTop = $(window).scrollTop(); | |
var step = Math.round((end - scrollTop) / scrollSpeed); | |
if (scrollTop <= 0 || | |
scrollTop >= $(window).prop("scrollHeight") - $(window).height() || | |
goUp && step > -1 || | |
!goUp && step < 1 ) { | |
clearInterval(interval); | |
interval = null; | |
end = null; | |
} | |
$(window).scrollTop(scrollTop + step ); | |
}, animationInterval); | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment