You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
644 B
28 lines
644 B
/**
|
|
* 监听 dom size 变化
|
|
* @param {*} target HTMLElement
|
|
* @param {*} callback 参数 rect
|
|
* @param {*} times n次后注销
|
|
* @returns 销毁函数
|
|
*/
|
|
export function onceObserver(target, callback, times = 2) {
|
|
let index = 1;
|
|
|
|
const resizeObserve = new ResizeObserver(function ([{ target }]) {
|
|
callback?.(target.getBoundingClientRect());
|
|
|
|
if (index++ > times - 1) this.disconnect();
|
|
});
|
|
|
|
resizeObserve.observe(target);
|
|
|
|
function destroyObserver() {
|
|
resizeObserve.disconnect();
|
|
}
|
|
|
|
if (typeof this.$once === "function") {
|
|
this.$once("hook:beforeDestroy", destroyObserver);
|
|
}
|
|
|
|
return destroyObserver;
|
|
}
|
|
|