iter(g106): 滚动监听改为卸载自清理
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user