[英]Detect when a DIV's height changes without polling or mutation observers
I need to get the height of a div
and get notified when its height changes according to data. 我需要获得
div
的高度,并在其高度根据数据变化时得到通知。
I am using getBoundingClientRect()
to get the height currently, but I don't get notified of any further changes. 我正在使用
getBoundingClientRect()
来获取当前的高度,但我没有得到任何进一步更改的通知。
I also tried DOMAttrModified
and MutationObserver
to detect changes in the data, but both of them are not universally supported in all browsers. 我还尝试使用
DOMAttrModified
和MutationObserver
来检测数据中的更改,但是并非所有浏览器都普遍支持这两种更改。
What's the best way to get notifications about changes to an element's height? 获取有关元素高度变化的通知的最佳方法是什么?
The idea (that seems to originate from a blog post on backalleycoder.com ) is that you can use 这个想法(似乎源于backalleycoder.com上的博客文章 )是你可以使用的
onresize
on the element itself in IE <=10. onresize
。 scroll
events on a specially crafted <div>
appended as a child of the element in most other browsers <div>
上scroll
事件 onresize
on <object style="position: absolute; top: 0; left: 0; height: 100%; width: 100%;">
appended as a child of the element. onresize
on <object style="position: absolute; top: 0; left: 0; height: 100%; width: 100%;">
附加为元素的子元素。 There's a number of libraries that implement some or all of these approaches: 有许多库实现了部分或全部这些方法:
onresize
in IE + scroll
in other browsers. onresize
在其他浏览器中scroll
。 Small (5KB unminified), but not actively maintained ATM. <object>
technique is only truly needed in certain older browsers. <object>
技术。 The author even wrote a paper about this ( Modular Responsive Web Design using Element Queries ). ResizeSensor
class that appears to use the same scroll
technique (from another popular SO question: How to detect DIV's dimension changed? ). scroll
技术的ResizeSensor
类(来自另一个流行的SO问题: 如何检测DIV的维度发生变化? )。 The rest of library has a different purpose, though. Note that older questions here on SO talk about: 请注意,SO上的旧问题谈到:
attrchange
in jQuery, DOMAttrModified, etc. - that won't help you with detecting height changes that do not involve changing the height
attribute attrchange
- 这对于检测不涉及更改height
属性的高度变化没有帮助 [edit] ResizeObserver will allow to detect this without hacks in newer browsers. [edit] ResizeObserver允许在没有新浏览器的黑客攻击的情况下检测到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.