Set style change event listener in Javascript

后端 未结 2 1088
旧巷少年郎
旧巷少年郎 2021-01-26 07:17

My div looks something like this:

2条回答
  •  -上瘾入骨i
    2021-01-26 07:32

    You can use MutationObserver

    Then code example:

    var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutationRecord) {
        console.log('style changed!');
       });    
     });
    
     var target = document.getElementById('myId');
      observer.observe(target, { attributes : true, attributeFilter : ['style'] });
    

    Note: this only works with inline styles(any changing of styles), not when the style changes as a consequence of class change or @media change This is a answer https://stackoverflow.com/a/20683311/3344953

提交回复
热议问题