tav-ui
Version:
53 lines (50 loc) • 1.29 kB
JavaScript
import { ref, unref } from 'vue';
import { isUnDef, isFunction } from '../../utils/is2.mjs';
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 move = (el, amount, direction) => {
el[direction] = amount;
};
const position = (el, direction) => {
return el[direction];
};
function useScrollTo({
el,
to,
duration = 500,
direction = "scrollTop",
callback
}) {
const isActiveRef = ref(false);
const start = position(el, direction);
const change = to - start;
const increment = 20;
let currentTime = 0;
duration = isUnDef(duration) ? 500 : duration;
const animateScroll = function() {
if (!unref(isActiveRef))
return;
currentTime += increment;
const val = easeInOutQuad(currentTime, start, change, duration);
move(el, val, direction);
if (currentTime < duration && unref(isActiveRef))
requestAnimationFrame(animateScroll);
else if (callback && isFunction(callback))
callback();
};
const run = () => {
isActiveRef.value = true;
animateScroll();
};
const stop = () => {
isActiveRef.value = false;
};
return { start: run, stop };
}
export { useScrollTo };
//# sourceMappingURL=useScrollTo2.mjs.map