UNPKG

2.26 kBJavaScriptView Raw
1var __defProp = Object.defineProperty;
2var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3var __getOwnPropNames = Object.getOwnPropertyNames;
4var __hasOwnProp = Object.prototype.hasOwnProperty;
5var __export = (target, all) => {
6 for (var name in all)
7 __defProp(target, name, { get: all[name], enumerable: true });
8};
9var __copyProps = (to, from, except, desc) => {
10 if (from && typeof from === "object" || typeof from === "function") {
11 for (let key of __getOwnPropNames(from))
12 if (!__hasOwnProp.call(to, key) && key !== except)
13 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14 }
15 return to;
16};
17var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18var stdin_exports = {};
19__export(stdin_exports, {
20 scrollLeftTo: () => scrollLeftTo,
21 scrollTopTo: () => scrollTopTo
22});
23module.exports = __toCommonJS(stdin_exports);
24var import_use = require("@vant/use");
25var import_utils = require("../utils");
26function scrollLeftTo(scroller, to, duration) {
27 let rafId;
28 let count = 0;
29 const from = scroller.scrollLeft;
30 const frames = duration === 0 ? 1 : Math.round(duration * 1e3 / 16);
31 let scrollLeft = from;
32 function cancel() {
33 (0, import_use.cancelRaf)(rafId);
34 }
35 function animate() {
36 scrollLeft += (to - from) / frames;
37 scroller.scrollLeft = scrollLeft;
38 if (++count < frames) {
39 rafId = (0, import_use.raf)(animate);
40 }
41 }
42 animate();
43 return cancel;
44}
45function scrollTopTo(scroller, to, duration, callback) {
46 let rafId;
47 let current = (0, import_utils.getScrollTop)(scroller);
48 const isDown = current < to;
49 const frames = duration === 0 ? 1 : Math.round(duration * 1e3 / 16);
50 const step = (to - current) / frames;
51 function cancel() {
52 (0, import_use.cancelRaf)(rafId);
53 }
54 function animate() {
55 current += step;
56 if (isDown && current > to || !isDown && current < to) {
57 current = to;
58 }
59 (0, import_utils.setScrollTop)(scroller, current);
60 if (isDown && current < to || !isDown && current > to) {
61 rafId = (0, import_use.raf)(animate);
62 } else if (callback) {
63 rafId = (0, import_use.raf)(callback);
64 }
65 }
66 animate();
67 return cancel;
68}