SHARE
TWEET

Untitled

a guest Mar 25th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Smooth scroll */
  2. Math.easeOut = function (t, b, c, d) { t /= d; return -c * t*(t-2) + b; };
  3.  
  4. (function() { // do not mess global space
  5. var
  6.   interval, // scroll is being eased
  7.   mult = 0, // how fast do we scroll
  8.   dir = 0, // 1 = scroll down, -1 = scroll up
  9.   steps = 50, // how many steps in animation
  10.   length = 30; // how long to animate
  11. function MouseWheelHandler(e) {
  12.   e.preventDefault(); // prevent default browser scroll
  13.   clearInterval(interval); // cancel previous animation
  14.   ++mult; // we are going to scroll faster
  15.   var delta = -Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
  16.   if(dir!=delta) { // scroll direction changed
  17.     mult = 1; // start slowly
  18.     dir = delta;
  19.   }
  20.   for(var tgt=e.target; tgt!=document.documentElement; tgt=tgt.parentNode) {
  21.     var oldScroll = tgt.scrollTop;
  22.     tgt.scrollTop+= delta;
  23.     if(oldScroll!=tgt.scrollTop) break;
  24.   }
  25.   var start = tgt.scrollTop;
  26.   var end = start + length*mult*delta; // where to end the scroll
  27.   var change = end - start; // base change in one step
  28.   var step = 0; // current step
  29.   interval = setInterval(function() {
  30.     var pos = Math.easeOut(step++,start,change,steps);
  31.     //window.scrollTo(0,pos);
  32.     tgt.scrollTop = pos;
  33.     if(step>=steps) { // scroll finished without speed up - stop by easing out
  34.       mult = 0;
  35.       clearInterval(interval);
  36.     }
  37.   },10);
  38. }
  39. window.addEventListener("mousewheel", MouseWheelHandler, false);
  40. window.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
  41. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top