简体   繁体   English

如何听 div 的高度变化?

[英]How can I listen to the height change of a div?

I have a container div whose height can change when new elements loaded in it or because the user resize the screen (it is responsive with flex-wrap).我有一个容器 div,当新元素加载到其中或因为用户调整屏幕大小(它通过 flex-wrap 响应)时,它的高度可以改变。 I need to know when the height of the div change to then execute a JS function.我需要知道 div 的高度何时更改,然后执行 JS function。 What event can I had with addEventListener?我可以使用 addEventListener 进行什么活动? Or maybe I read about Observers but not sure this is the solution.或者也许我读到了 Observers 但不确定这是解决方案。

You can use aResizeObserver .您可以使用ResizeObserver

The ResizeObserver interface reports changes to the dimensions of an Element's content or border box, or the bounding box of an SVGElement . ResizeObserver接口报告元素内容或边框框或SVGElement边界框的尺寸变化。

It's supported by all major browsers .所有主流浏览器都支持它。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM