Synopsis
✓ Published0🌍 Public
Aabernier
Last edited Mar 25, 2015
Created on Mar 25, 2015
This example shows a DOM element’s CSS style properties being monitored for changes, logging each modification with its old and new values. It demonstrates both a single-property listener and a multi-property change handler. The code uses Backbone.Events for event handling, MutationObserver to detect style attribute mutations, and Underscore for utility functions. The demo binds to `transform-origin` and `border-radius` on a div, then programmatically updates `transform-origin` after one second, triggering the change callbacks.
AI-generated descriptionWatch a DOM element for [style] css properties changes.
It uses MutationObserver internally
Synopsis
new Watchcss(el, ['propA', 'propB', ...])
.observe()
.disconnect()
.on('change', function (changes) {})
.on('change:propA', function (change) {})
Usage
var el = document.getElementById('foo');
var watchcss = new Watchcss(el, ['transform-origin', 'border-radius']);
watchcss.on('change', function (changes) {
console.log('transform-origin OR border-radius have changed!', changes);
});
outputs:
[
{
propName: 'transform-origin',
oldValue: 'center center',
newValue: 'left center'
},
{
propName: 'border-radius',
oldValue: '3px',
newValue: '10px'
}
]
or with a single property:
watchcss.on('change:transform-origin', function (change) {
console.log('transform-origin has changed!', change);
});
outputs:
{
propName: 'transform-origin',
oldValue: 'center center',
newValue: 'left center'
}
ISC Licensed