Created
March 8, 2017 09:27
-
-
Save marcelweder/89b7ce6569571d347aa5ac1b1c918693 to your computer and use it in GitHub Desktop.
One of the whorst JS-Scroll 'Handler' ever. Don't use this shit! 😤
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
/*** Smooth scroll (IE-Scroll-Fix) ***/ | |
Math.easeOut = function(t, b, c, d) { | |
t /= d; | |
return -c * t * (t - 2) + b; | |
}; | |
(function() { | |
// do not mess global space | |
var | |
interval, | |
// scroll is being eased | |
mult = 0, | |
// how fast do we scroll | |
dir = 0, | |
// 1 = scroll down, -1 = scroll up | |
steps = 30, | |
// how many steps in animation | |
length = 50; // how long to animate | |
function MouseWheelHandler(e) { | |
e.preventDefault(); // prevent default browser scroll | |
clearInterval(interval); // cancel previous animation | |
++mult; // we are going to scroll faster | |
var delta = -Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))); | |
if (dir != delta) { | |
// scroll direction changed | |
mult = 1; // start slowly | |
dir = delta; | |
} | |
for (var tgt = e.target; tgt != document.documentElement; tgt = tgt.parentNode) { | |
var oldScroll = tgt.scrollTop; | |
tgt.scrollTop += delta; | |
if (oldScroll != tgt.scrollTop) | |
break; | |
} | |
var start = tgt.scrollTop; | |
var end = start + length * mult * delta; // where to end the scroll | |
var change = end - start; // base change in one step | |
var step = 0; // current step | |
interval = setInterval(function() { | |
var pos = Math.easeOut(step++, start, change, steps); | |
//window.scrollTo(0,pos); | |
tgt.scrollTop = pos; | |
if (step >= steps) { | |
// scroll finished without speed up - stop by easing out | |
mult = 0; | |
clearInterval(interval); | |
} | |
}, 10); | |
} | |
if (window.addEventListener) { | |
window.addEventListener("mousewheel", MouseWheelHandler, false); | |
window.addEventListener("DOMMouseScroll", MouseWheelHandler, false); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment