如何使用JS监听某一元素样式的变化

Posted by JP on June 15, 2022

使用下面的js脚本,即可监听element样式的变化

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'] });

Reference: