vue-admin-core
Version:
A Component Library for Vue 3
45 lines (43 loc) • 1.31 kB
JavaScript
const easeInOutQuad = (t, b, c, d) => {
t /= d / 2;
if (t < 1) {
return c / 2 * t * t + b;
}
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
};
const requestAnimFrame = function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) {
window.setTimeout(callback, 1e3 / 60);
};
}();
const move = (amount) => {
document.documentElement.scrollTop = amount;
document.body.parentNode.scrollTop = amount;
document.body.scrollTop = amount;
};
const position = () => {
return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop;
};
const scrollTo = (to, duration, callback) => {
const start = position();
const change = to - start;
const increment = 20;
let currentTime = 0;
duration = typeof duration === "undefined" ? 500 : duration;
const animateScroll = function() {
currentTime += increment;
const val = easeInOutQuad(currentTime, start, change, duration);
move(val);
if (currentTime < duration) {
requestAnimFrame(animateScroll);
} else {
if (callback && typeof callback === "function") {
callback();
}
}
};
animateScroll();
};
export { scrollTo };
//# sourceMappingURL=scrollTo.mjs.map