utils.mjs 1018 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { raf } from "@vant/use";
  2. import { getScrollTop, setScrollTop } from "../utils/index.mjs";
  3. function scrollLeftTo(scroller, to, duration) {
  4. let count = 0;
  5. const from = scroller.scrollLeft;
  6. const frames = duration === 0 ? 1 : Math.round(duration * 1e3 / 16);
  7. function animate() {
  8. scroller.scrollLeft += (to - from) / frames;
  9. if (++count < frames) {
  10. raf(animate);
  11. }
  12. }
  13. animate();
  14. }
  15. function scrollTopTo(scroller, to, duration, callback) {
  16. let current = getScrollTop(scroller);
  17. const isDown = current < to;
  18. const frames = duration === 0 ? 1 : Math.round(duration * 1e3 / 16);
  19. const step = (to - current) / frames;
  20. function animate() {
  21. current += step;
  22. if (isDown && current > to || !isDown && current < to) {
  23. current = to;
  24. }
  25. setScrollTop(scroller, current);
  26. if (isDown && current < to || !isDown && current > to) {
  27. raf(animate);
  28. } else if (callback) {
  29. raf(callback);
  30. }
  31. }
  32. animate();
  33. }
  34. export {
  35. scrollLeftTo,
  36. scrollTopTo
  37. };