perf: 优化水印 hook 性能
This commit is contained in:
parent
d2952e0d16
commit
f3e71e0795
@ -56,11 +56,11 @@ export function useWatermark(parentEl: Ref<HTMLElement | null> = bodyEl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 刷新水印(防御时调用) */
|
/** 刷新水印(防御时调用) */
|
||||||
const updateWatermark = () => {
|
const updateWatermark = debounce(() => {
|
||||||
clearWatermark()
|
clearWatermark()
|
||||||
createWatermarkEl()
|
createWatermarkEl()
|
||||||
addParentElListener(parentEl.value!)
|
addParentElListener(parentEl.value!)
|
||||||
}
|
}, 500)
|
||||||
|
|
||||||
/** 创建水印元素 */
|
/** 创建水印元素 */
|
||||||
const createWatermarkEl = () => {
|
const createWatermarkEl = () => {
|
||||||
@ -155,7 +155,7 @@ export function useWatermark(parentEl: Ref<HTMLElement | null> = bodyEl) {
|
|||||||
subtree: true
|
subtree: true
|
||||||
}
|
}
|
||||||
// 当观察到变动时执行的回调
|
// 当观察到变动时执行的回调
|
||||||
const mutationCallback = (mutationList: MutationRecord[]) => {
|
const mutationCallback = debounce((mutationList: MutationRecord[]) => {
|
||||||
// 水印的防御(防止用户手动删除水印元素或通过 CSS 隐藏水印)
|
// 水印的防御(防止用户手动删除水印元素或通过 CSS 隐藏水印)
|
||||||
mutationList.forEach((mutation) => {
|
mutationList.forEach((mutation) => {
|
||||||
switch (mutation.type) {
|
switch (mutation.type) {
|
||||||
@ -165,11 +165,13 @@ export function useWatermark(parentEl: Ref<HTMLElement | null> = bodyEl) {
|
|||||||
})
|
})
|
||||||
break
|
break
|
||||||
case "attributes":
|
case "attributes":
|
||||||
|
if (mutation.target === watermarkEl) {
|
||||||
updateWatermark()
|
updateWatermark()
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}, 500)
|
||||||
// 创建一个观察器实例并传入回调
|
// 创建一个观察器实例并传入回调
|
||||||
const mutationObserver = new MutationObserver(mutationCallback)
|
const mutationObserver = new MutationObserver(mutationCallback)
|
||||||
// 以上述配置开始观察目标节点
|
// 以上述配置开始观察目标节点
|
||||||
|
Loading…
x
Reference in New Issue
Block a user