文档
- https://developer.mozilla.org/zh-CN/docs/Web/API/ResizeObserver
- https://github.com/que-etc/resize-observer-polyfill
文档描述
- ResizeObserver 接口监视 Element 内容盒或边框盒或者 SVGElement 边界尺寸的变化。
使用示例
<style> #box { height: 200px; background-color: #808080; } </style> <div id="box"></div> <script> const resizeObserver = new ResizeObserver((entries) => { for (const entry of entries) { console.log(entry.contentRect.width); } }); resizeObserver.observe(document.querySelector("#box")); </script>
在线Demo: https://mouday.github.io/front-end-demo/ResizeObserver-demo.html