UNPKG

tav-ui

Version:
57 lines (52 loc) 1.37 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var vue = require('vue'); var is = require('../../utils/is2.js'); 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 = vue.ref(false); const start = position(el, direction); const change = to - start; const increment = 20; let currentTime = 0; duration = is.isUnDef(duration) ? 500 : duration; const animateScroll = function() { if (!vue.unref(isActiveRef)) return; currentTime += increment; const val = easeInOutQuad(currentTime, start, change, duration); move(el, val, direction); if (currentTime < duration && vue.unref(isActiveRef)) requestAnimationFrame(animateScroll); else if (callback && is.isFunction(callback)) callback(); }; const run = () => { isActiveRef.value = true; animateScroll(); }; const stop = () => { isActiveRef.value = false; }; return { start: run, stop }; } exports.useScrollTo = useScrollTo; //# sourceMappingURL=useScrollTo2.js.map