小程序初始提交
This commit is contained in:
33
cool-unix/cool/scroller/index.ts
Normal file
33
cool-unix/cool/scroller/index.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { router } from "../router";
|
||||
|
||||
class Scroller {
|
||||
list: Map<string, ((top: number) => void)[]> = new Map();
|
||||
|
||||
// 触发滚动
|
||||
emit(top: number) {
|
||||
const cbs = this.list.get(router.path()) ?? [];
|
||||
cbs.forEach((cb) => {
|
||||
cb(top);
|
||||
});
|
||||
}
|
||||
|
||||
// 监听页面滚动
|
||||
on(callback: (top: number) => void) {
|
||||
const path = router.path();
|
||||
const cbs = this.list.get(path) ?? [];
|
||||
cbs.push(callback);
|
||||
this.list.set(path, cbs);
|
||||
}
|
||||
|
||||
// 取消监听页面滚动
|
||||
off = (callback: (top: number) => void) => {
|
||||
const path = router.path();
|
||||
const cbs = this.list.get(path) ?? [];
|
||||
this.list.set(
|
||||
path,
|
||||
cbs.filter((cb) => cb != callback)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export const scroller = new Scroller();
|
||||
Reference in New Issue
Block a user