2012-08-30 24 views
6

¿Alguna implementación sugerida de un detector de cambio de propiedad CSS? Tal vez:CSS Property Change Listener

thread = 

function getValues(){ 
    while(true){ 
    for each CSS property{ 
     if(properties[property] != nil && getValue(property) != properties[property]){alert('change')} 
     else{properties[property] = getValue(property)} 
    } 
    } 
} 

Respuesta

3

Creo que estás buscando este:

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

Si google para ello, un montón de cosas aparece. Esto parece prometedor, aunque:

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

+0

Ésta no funciona en los navegadores basados ​​en WebKit, Chrome, al menos. –

2

eventos de mutación como DOMAttrModified están en desuso. Considere usar un MutationObserver en su lugar.

Ejemplo:

<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);