优化页面滚动事件

This commit is contained in:
icssoa
2025-08-11 23:48:02 +08:00
parent f2621341a4
commit 407c7b0521
8 changed files with 119 additions and 74 deletions

View File

@@ -1,8 +1,12 @@
import { scroller } from "./scroller";
import { initTheme, setH5 } from "./theme";
import { initLocale } from "@/locale";
export function cool(app: VueApp) {
app.mixin({
onPageScroll(e) {
scroller.emit(e.scrollTop);
},
onShow() {
// #ifdef H5
setTimeout(() => {
@@ -18,11 +22,12 @@ export function cool(app: VueApp) {
console.log(app);
}
export * from "./utils";
export * from "./theme";
export * from "./router";
export * from "./service";
export * from "./hooks";
export * from "./ctx";
export * from "./hooks";
export * from "./router";
export * from "./scroller";
export * from "./service";
export * from "./store";
export * from "./theme";
export * from "./upload";
export * from "./utils";

23
cool/scroller/index.ts Normal file
View File

@@ -0,0 +1,23 @@
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);
}
}
export const scroller = new Scroller();