我要查看<body>
的宽度变化(不是高度)。
有没有办法用 ResizeObserver 做到这一点?
最佳答案
这是一个例子。在 https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver 上阅读更多文档.
function handleWidthChange (width) {
console.log(width);
}
let prevWidth = 0;
const observer = new ResizeObserver(entries => {
for (const entry of entries) {
const width = entry.borderBoxSize?.[0].inlineSize;
if (typeof width === 'number' && width !== prevWidth) {
prevWidth = width;
handleWidthChange(width);
}
}
});
observer.observe(document.body, {box: 'border-box'});
关于javascript - 如何使用 ResizeObserver 仅检查 JavaScript 中正文的宽度变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68629538/