index.ts 477 B

1234567891011121314151617181920212223
  1. import { router } from "../router";
  2. class Scroller {
  3. list: Map<string, ((top: number) => void)[]> = new Map();
  4. // 触发滚动
  5. emit(top: number) {
  6. const cbs = this.list.get(router.path()) ?? [];
  7. cbs.forEach((cb) => {
  8. cb(top);
  9. });
  10. }
  11. // 监听页面滚动
  12. on(callback: (top: number) => void) {
  13. const path = router.path();
  14. const cbs = this.list.get(path) ?? [];
  15. cbs.push(callback);
  16. this.list.set(path, cbs);
  17. }
  18. }
  19. export const scroller = new Scroller();