page-scroll.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { getCurrentPage, isDef } from '../common/utils';
  2. function onPageScroll(event) {
  3. const { vanPageScroller = [] } = getCurrentPage();
  4. vanPageScroller.forEach((scroller) => {
  5. if (typeof scroller === 'function') {
  6. // @ts-ignore
  7. scroller(event);
  8. }
  9. });
  10. }
  11. export const pageScrollMixin = (scroller) => Behavior({
  12. attached() {
  13. const page = getCurrentPage();
  14. if (!isDef(page)) {
  15. return;
  16. }
  17. if (Array.isArray(page.vanPageScroller)) {
  18. page.vanPageScroller.push(scroller.bind(this));
  19. }
  20. else {
  21. page.vanPageScroller =
  22. typeof page.onPageScroll === 'function'
  23. ? [page.onPageScroll.bind(page), scroller.bind(this)]
  24. : [scroller.bind(this)];
  25. }
  26. page.onPageScroll = onPageScroll;
  27. },
  28. detached() {
  29. var _a;
  30. const page = getCurrentPage();
  31. if (isDef(page)) {
  32. page.vanPageScroller =
  33. ((_a = page.vanPageScroller) === null || _a === void 0 ? void 0 : _a.filter((item) => item !== scroller)) || [];
  34. }
  35. },
  36. });