Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Property Change Listener

Tags:

javascript

css

Any suggested implementations of a CSS property change listener? Maybe:

thread =

function getValues(){
  while(true){
    for each CSS property{
      if(properties[property] != nil && getValue(property) != properties[property]){alert('change')}
      else{properties[property] = getValue(property)}
    }
  }
}
like image 498
Drew Avatar asked Aug 30 '12 16:08

Drew


2 Answers

Mutation events like DOMAttrModified are deprecated. Consider using a MutationObserver instead.

Example:

<div>use devtools to change the <code>background-color</code> property of this node to <code>red</code></div>
<p>status...</p>

JS:

var observer = new MutationObserver((mutations) => {
  mutations.forEach((mutation) => {
    if (mutation.target.style.color === 'red') {
      document.querySelector('p').textContent = 'success';
    }
  });
});

var observerConfig = {
  attributes: true,
  childList: false,
  characterData: false,
  attributeOldValue: true
};

var targetNode = document.querySelector('div');
observer.observe(targetNode, observerConfig);
like image 136
Kayce Basques Avatar answered Oct 15 '22 10:10

Kayce Basques


I think you're looking for this:

document.documentElement.addEventListener('DOMAttrModified', function(e){
  if (e.attrName === 'style') {
    console.log('prevValue: ' + e.prevValue, 'newValue: ' + e.newValue);
  }
}, false);

If you google for it, a bunch of stuff comes up. This looks promising though:

http://darcyclarke.me/development/detect-attribute-changes-with-jquery/

like image 25
Swift Avatar answered Oct 15 '22 11:10

Swift