javascript - 如何使用 ResizeObserver 仅检查 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'});
推荐阅读
- python - 如何重载获取 self 类对象的 __eq__ 函数
- php - 尝试将外部 API 与 Wordpress 链接
- python - 信息:找不到给定模式的文件。错误
- python - 使用不规则 xyz 数据制作等高线图
- c++ - OOP - 如何在父类中调用子方法
- ruby-on-rails - Rails 6、Nginx、Passenger、Mysql、rbenv
- php - 如何使用 Http 客户端(可选更改)将 Symfony 请求转发到另一台服务器?
- python - 通过卷积神经网络进行图像着色 - 一次迭代中所有图片的预测 a 和 b 值相同
- python - python中的difflib get_close_matches错误
- types - 如何更改 jupyter notebook 的默认主题(jt-r)的字体大小和类型?