admin管理员组文章数量:1394207
Can I use a MutationObserver to listen for changes on puted styles? I have a div which width is 100% and I'd like to know when its puted width changes, but so far no success. The MutationObserver works if I change the style pragmatically:
document.getElementById("myDiv").style.width = "200px"
But it doesn't work if the interface is resized by a window resize or if a parent div resizes.
Any solution that doesn't involve timeouts?
UPDATE: I'm particularly interested in a solution that also wouldn't involve listening to window resize event, because sometimes the interface changes without the window resizing. For instance, a div that has a percentage as width and a parent node size changes.
Can I use a MutationObserver to listen for changes on puted styles? I have a div which width is 100% and I'd like to know when its puted width changes, but so far no success. The MutationObserver works if I change the style pragmatically:
document.getElementById("myDiv").style.width = "200px"
But it doesn't work if the interface is resized by a window resize or if a parent div resizes.
Any solution that doesn't involve timeouts?
UPDATE: I'm particularly interested in a solution that also wouldn't involve listening to window resize event, because sometimes the interface changes without the window resizing. For instance, a div that has a percentage as width and a parent node size changes.
Share Improve this question edited Sep 25, 2015 at 2:31 lbrandao asked Sep 23, 2015 at 18:01 lbrandaolbrandao 4,3734 gold badges37 silver badges44 bronze badges 6-
give a try with jquery's
width()
function – Vineet Commented Sep 23, 2015 at 18:06 - it has to be automatic – lbrandao Commented Sep 23, 2015 at 18:10
- yes width function will calculate the width dynamically – Vineet Commented Sep 23, 2015 at 18:11
- sorry buddy, but you didn't understand the question, I want to know WHEN the size changes! – lbrandao Commented Sep 23, 2015 at 18:15
- relevant - stackoverflow./questions/6492683/… – James LeClair Commented Sep 23, 2015 at 18:18
1 Answer
Reset to default 7Support for observing style changes is in discussion. For now you could leverage transitions. The idea is to add a css transition for the width (or whatever) to the element. The transition should have a duration of almost zero, so you won't notice it.
After a transition has finished a transitionend
event is fired. Add a listener for that event. Now whenever the width changes, the transition will start, immediately finish and the event will fire. Which means your listener will be called.
#myDiv {
...
transition: width 0.01s;
}
$("#myDiv").on("transitionend", function () { ... } );
Supported by IE10+
本文标签: javascriptCan I use a MutationObserver to listen for changes on computed stylesStack Overflow
版权声明:本文标题:javascript - Can I use a MutationObserver to listen for changes on computed styles? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744582232a2613989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论