|
@@ -1,35 +1,34 @@
|
|
|
-import { isFunction, isUnDef } from '/@/utils/is';
|
|
|
-import { ref, unref } from 'vue';
|
|
|
+import { shallowRef, unref } from 'vue';
|
|
|
|
|
|
-export interface ScrollToParams {
|
|
|
+interface UseScrollToOptions {
|
|
|
el: any;
|
|
|
to: number;
|
|
|
duration?: number;
|
|
|
callback?: () => any;
|
|
|
}
|
|
|
|
|
|
-const easeInOutQuad = (t: number, b: number, c: number, d: number) => {
|
|
|
+function easeInOutQuad(t: number, b: number, c: number, d: number) {
|
|
|
t /= d / 2;
|
|
|
if (t < 1) {
|
|
|
return (c / 2) * t * t + b;
|
|
|
}
|
|
|
t--;
|
|
|
return (-c / 2) * (t * (t - 2) - 1) + b;
|
|
|
-};
|
|
|
-const move = (el: HTMLElement, amount: number) => {
|
|
|
+}
|
|
|
+
|
|
|
+function move(el: HTMLElement, amount: number) {
|
|
|
el.scrollTop = amount;
|
|
|
-};
|
|
|
+}
|
|
|
|
|
|
const position = (el: HTMLElement) => {
|
|
|
return el.scrollTop;
|
|
|
};
|
|
|
-export function useScrollTo({ el, to, duration = 500, callback }: ScrollToParams) {
|
|
|
- const isActiveRef = ref(false);
|
|
|
+function useScrollTo({ el, to, duration = 500, callback }: UseScrollToOptions) {
|
|
|
+ const isActiveRef = shallowRef(false);
|
|
|
const start = position(el);
|
|
|
const change = to - start;
|
|
|
const increment = 20;
|
|
|
let currentTime = 0;
|
|
|
- duration = isUnDef(duration) ? 500 : duration;
|
|
|
|
|
|
const animateScroll = function () {
|
|
|
if (!unref(isActiveRef)) {
|
|
@@ -41,7 +40,7 @@ export function useScrollTo({ el, to, duration = 500, callback }: ScrollToParams
|
|
|
if (currentTime < duration && unref(isActiveRef)) {
|
|
|
requestAnimationFrame(animateScroll);
|
|
|
} else {
|
|
|
- if (callback && isFunction(callback)) {
|
|
|
+ if (callback && typeof callback === 'function') {
|
|
|
callback();
|
|
|
}
|
|
|
}
|
|
@@ -57,3 +56,5 @@ export function useScrollTo({ el, to, duration = 500, callback }: ScrollToParams
|
|
|
|
|
|
return { start: run, stop };
|
|
|
}
|
|
|
+
|
|
|
+export { useScrollTo, type UseScrollToOptions };
|