DOM元素的计算样式更改时触发事件? [英] Fire an event when DOM element's computed style changes?
问题描述
Is there a non-polling method to listen for changes to an element's computed style?
这个幻想代码段应该简洁地解释我的意思:
This fantasy code snippet should explain what I mean succinctly:
var el = document.getElementById('doodad');
el.addComputedStyleChangeListener('width', function (prev, new) {
alert('Previous width: ' + prev + '; New width: ' + new);
});
我知道 DOMAttrModified
突变事件和即将推出的 MutationObserver
> ,但这还不够—它们只能用于监视元素的 style
DOM属性,这不能完全确定元素的计算样式.
I'm aware of the DOMAttrModified
mutation event and the upcoming MutationObserver
, but neither is sufficient -- they can only be used to watch the style
DOM attribute of an element, which doesn't wholly determine an element's computed style.
用例最初是这个问题,这确实使我产生了好奇心.
The use case for this was originally part of this question, which really just lead me down a course of curiosity.
推荐答案
没有这种方法. CSS OM 尚不存在.
There is no such method. CSS OM is not there yet.
目前尚不清楚计算样式更改"是什么意思.
And it is not clear what "computed style change" means.
原则上,您可以检测到所用(例如,渲染)样式的变化.但这需要发生画图"或布局"之类的事件.
In principle you can detect change of used (for e.g. rendering) style. But this will need some event like "paint" or "layout" to happen.
这篇关于DOM元素的计算样式更改时触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!