utils.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __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. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. scrollLeftTo: () => scrollLeftTo,
  21. scrollTopTo: () => scrollTopTo
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_use = require("@vant/use");
  25. var import_utils = require("../utils");
  26. function 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. }
  45. function 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. }