utils.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 count = 0;
  28. const from = scroller.scrollLeft;
  29. const frames = duration === 0 ? 1 : Math.round(duration * 1e3 / 16);
  30. function animate() {
  31. scroller.scrollLeft += (to - from) / frames;
  32. if (++count < frames) {
  33. (0, import_use.raf)(animate);
  34. }
  35. }
  36. animate();
  37. }
  38. function scrollTopTo(scroller, to, duration, callback) {
  39. let current = (0, import_utils.getScrollTop)(scroller);
  40. const isDown = current < to;
  41. const frames = duration === 0 ? 1 : Math.round(duration * 1e3 / 16);
  42. const step = (to - current) / frames;
  43. function animate() {
  44. current += step;
  45. if (isDown && current > to || !isDown && current < to) {
  46. current = to;
  47. }
  48. (0, import_utils.setScrollTop)(scroller, current);
  49. if (isDown && current < to || !isDown && current > to) {
  50. (0, import_use.raf)(animate);
  51. } else if (callback) {
  52. (0, import_use.raf)(callback);
  53. }
  54. }
  55. animate();
  56. }