Skip to content

Instantly share code, notes, and snippets.

@typoerr
Created June 4, 2016 01:02
Show Gist options
  • Save typoerr/1684965ed3dd3a9e08de7e7df8918356 to your computer and use it in GitHub Desktop.
Save typoerr/1684965ed3dd3a9e08de7e7df8918356 to your computer and use it in GitHub Desktop.
smooth_scroll.js
/*
参考: [javascript - ScrollTo with animation - Stack Overflow](http://stackoverflow.com/questions/12199363/scrollto-with-animation)
*/
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
/**
* (description)
*
* @export
* @param {number} scrollTargetY - the target scrollY property of the window
* @param {number} speed - time in pixels per second
* @param {string} easing - easing equation to use
*/
export default function scrollToY(scrollTargetY = 0, speed = 2000, easing = "easeOutSine") {
let scrollY = window.scrollY;
let currentTime = 0;
// min time .1, max time .8 seconds
const time = Math.max(.1, Math.min(Math.abs(scrollY - scrollTargetY) / speed, .8));
// easing equations from https://github.com/danro/easing-js/blob/master/easing.js
// var PI_D2 = Math.PI / 2;
const easingEquations = {
easeOutSine: function (pos) {
return Math.sin(pos * (Math.PI / 2));
},
easeInOutSine: function (pos) {
return (-0.5 * (Math.cos(Math.PI * pos) - 1));
},
easeInOutQuint: function (pos) {
if ((pos /= 0.5) < 1) {
return 0.5 * Math.pow(pos, 5);
}
return 0.5 * (Math.pow((pos - 2), 5) + 2);
}
};
// add animation loop
function tick() {
currentTime += 1 / 60;
var p = currentTime / time;
var t = easingEquations[easing](p);
if (p < 1) {
window.requestAnimFrame(tick);
window.scrollTo(0, scrollY + ((scrollTargetY - scrollY) * t));
} else {
window.scrollTo(0, scrollTargetY);
}
}
// call it once to get started
tick();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment