page-scroll.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const getCurrentPage = function () {
  2. const pages = getCurrentPages();
  3. return pages[pages.length - 1];
  4. };
  5. const onPageScroll = function (event) {
  6. const page = getCurrentPage();
  7. if (!page)
  8. return;
  9. const { pageScroller } = page;
  10. pageScroller.forEach((scroller) => {
  11. if (typeof scroller === 'function') {
  12. scroller(event);
  13. }
  14. });
  15. };
  16. export const pageScrollMixin = (scroller) => {
  17. let bindScroller = scroller;
  18. return Behavior({
  19. attached() {
  20. const page = getCurrentPage();
  21. if (!page)
  22. return;
  23. bindScroller = scroller.bind(this);
  24. if (Array.isArray(page.pageScroller)) {
  25. page.pageScroller.push(bindScroller);
  26. }
  27. else {
  28. page.pageScroller =
  29. typeof page.onPageScroll === 'function' ? [page.onPageScroll.bind(page), bindScroller] : [bindScroller];
  30. }
  31. page.onPageScroll = onPageScroll;
  32. },
  33. detached() {
  34. var _a;
  35. const page = getCurrentPage();
  36. if (!page)
  37. return;
  38. page.pageScroller = ((_a = page.pageScroller) === null || _a === void 0 ? void 0 : _a.filter((item) => item !== scroller)) || [];
  39. },
  40. });
  41. };
  42. export const getRect = function (context, selector) {
  43. return new Promise((resolve) => {
  44. wx.createSelectorQuery()
  45. .in(context)
  46. .select(selector)
  47. .boundingClientRect()
  48. .exec((rect = []) => resolve(rect[0]));
  49. });
  50. };