3 Star 54 Fork 14

Yaohaixiao / dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
observeResize.js 1.00 KB
一键复制 编辑 原始数据 按行查看 历史
Yaohaixiao 提交于 2024-04-17 18:21 . feat: 添加 observeResize() 方法
import debounce from './utils/lang/debounce'
import isFunction from './utils/types/isFunction'
import isElement from './isElement'
/**
* 通用的 ResizeObserver 观察者处理器
* ========================================================================
* @method observeResize
* @since 1.8.0
* @see https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/ResizeObserverEntry/contentBoxSize
* @see https://developer.mozilla.org/zh-CN/docs/Web/CSS/writing-mode
* @param {HTMLElement} el
* @param {Function} callback
* @param {Number} [delay]
* @return {ResizeObserver|boolean}
*/
const observeResize = (el, callback, delay = 300) => {
let observer
let fn
if (!isElement(el) || !isFunction(callback)) {
return false
}
fn = debounce(callback, delay)
observer = new ResizeObserver((entries) => {
for (const entry of entries) {
fn(entry)
}
})
observer.observe(el)
return observer
}
export default observeResize
JavaScript
1
https://gitee.com/yaohaixiao/dom.js.git
git@gitee.com:yaohaixiao/dom.js.git
yaohaixiao
dom.js
dom.js
main

搜索帮助