@aplus-frontend/ui
Version:
52 lines (51 loc) • 1.27 kB
JavaScript
import { useRafFn as R } from "@vueuse/core";
import "../../scroll-bar/index.mjs";
import "../../utils/index.mjs";
import { ensureRangeValue as s } from "../../scroll-bar/utils/index.mjs";
import { isDef as f } from "../../utils/is.mjs";
const g = () => {
}, b = (i) => 1 - Math.pow(1 - i, 3), X = ({
scrollOffset: i,
maxScrollDistance: r,
timer: l = 500,
onScroll: m = g,
onScrollEnd: T = g
}) => {
let u, e, t, o, n;
const { pause: a, resume: w, isActive: p } = R(
() => {
const v = Date.now() - u, c = s(v / l, 0, 1), y = b(c);
c === 1 ? (o && (i.x = t.x), n && (i.y = t.y), a(), m(o, n), T(o, n)) : (o && (t.x > e.x ? i.x = s(
e.x + (t.x - e.x) * y,
e.x,
r.x
) : i.x = s(
t.x + (e.x - t.x) * (1 - y),
0,
e.x
)), n && (t.y > e.y ? i.y = s(
e.y + (t.y - e.y) * y,
e.y,
r.y
) : i.y = s(
t.y + (e.y - t.y) * (1 - y),
0,
e.y
)), m(o, n));
},
{ immediate: !1 }
);
function d(x) {
p.value && a(), u = Date.now(), e = {
x: i.x,
y: i.y
}, t = x, o = f(x.x), n = f(x.y), w();
}
function h() {
a();
}
return { start: d, stop: h, isActive: p };
};
export {
X as useScrollSmooth
};