Can I use a MutationObserver to listen for changes on computed styles?

前端 未结 1 1588
一生所求
一生所求 2021-01-20 06:35

Can I use a MutationObserver to listen for changes on computed styles? I have a div which width is 100% and I\'d like to know when its computed width changes, but so far no

相关标签:
1条回答
  • 2021-01-20 07:01

    Support 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+

    0 讨论(0)
提交回复
热议问题