如何使用 ResizeObserver 仅检查 JavaScript 中 body 的宽度变化?
How to use ResizeObserver to check only body's width change in JavaScript?
我想检查 <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'});
我想检查 <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'});