page-scroll.js 959 B

123456789101112131415161718192021222324252627282930313233
  1. function getCurrentPage() {
  2. const pages = getCurrentPages();
  3. return pages[pages.length - 1] || {};
  4. }
  5. function onPageScroll(event) {
  6. const { vanPageScroller = [] } = getCurrentPage();
  7. vanPageScroller.forEach((scroller) => {
  8. if (typeof scroller === 'function') {
  9. scroller(event);
  10. }
  11. });
  12. }
  13. export const pageScrollMixin = (scroller) =>
  14. Behavior({
  15. attached() {
  16. const page = getCurrentPage();
  17. if (Array.isArray(page.vanPageScroller)) {
  18. page.vanPageScroller.push(scroller.bind(this));
  19. } else {
  20. page.vanPageScroller =
  21. typeof page.onPageScroll === 'function'
  22. ? [page.onPageScroll.bind(page), scroller.bind(this)]
  23. : [scroller.bind(this)];
  24. }
  25. page.onPageScroll = onPageScroll;
  26. },
  27. detached() {
  28. const page = getCurrentPage();
  29. page.vanPageScroller = (page.vanPageScroller || []).filter(
  30. (item) => item !== scroller
  31. );
  32. },
  33. });