iter(g106): 滚动监听改为卸载自清理

This commit is contained in:
HIsheR
2026-10-26 13:58:22 +08:00
parent 7d57f261df
commit c66bcf35da
6 changed files with 31 additions and 11 deletions
+2 -3
View File
@@ -1,5 +1,6 @@
import { h } from '@ui-arch/frame';
import type { AnchorItem, AnchorProps } from './types.js';
import { watchViewport } from './watch.js';
/*** 锚点导航:竖排区块链接,滚动时高亮当前区块,点击平滑跳转(尊重减少动效偏好) ***/
export function anchor(props: AnchorProps): HTMLElement {
@@ -70,8 +71,6 @@ export function anchor(props: AnchorProps): HTMLElement {
}
const host: HTMLElement | Window = scroller ?? window;
host.addEventListener('scroll', sync, { passive: true }); // 被动监听:不阻塞滚动
window.addEventListener('resize', sync); // 视口变化会改变判定线
sync(); // 首屏同步一次,刷新后停在中间位置也能高亮正确项
watchViewport(root, host, sync); // 滚动 / 缩放后同步高亮,节点脱离文档即自摘监听
return root;
}