Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire an event when DOM element's computed style changes?

Is there a non-polling method to listen for changes to an element's computed style?

This fantasy code snippet should explain what I mean succinctly:

var el = document.getElementById('doodad');

el.addComputedStyleChangeListener('width', function (prev, new) {
  alert('Previous width: ' + prev + '; New width: ' + new);
});

I'm aware of the DOMAttrModified mutation event and the upcoming MutationObserver, but neither is sufficient -- they can only be used to watch the style DOM attribute of an element, which doesn't wholly determine an element's computed style.


The use case for this was originally part of this question, which really just lead me down a course of curiosity.

like image 596
namuol Avatar asked Nov 01 '12 22:11

namuol


1 Answers

There is no such method. CSS OM is not there yet.

And it is not clear what "computed style change" means.

In principle you can detect change of used (for e.g. rendering) style. But this will need some event like "paint" or "layout" to happen.

like image 153
c-smile Avatar answered Sep 19 '22 23:09

c-smile