下面是觀察屬性的元素的一個例子。
見:MutationObserver
和Mutation Events
CSS
#watch {
border: 1px solid;
}
HTML
<button id="button">Click me</button>
<div id="watch" style="width: 100px; height: 50px;"></div>
的Javascript
/*jslint sub: true, maxerr: 50, indent: 4, browser: true */
/* global global */
(function (global) {
"use strict";
if (typeof global.MutationObserver !== "function") {
global.MutationObserver = global.WebKitMutationObserver || global.MozMutationObserver;
}
var watch = document.getElementById("watch");
function whenClicked() {
watch.style.width = "200px";
}
document.getElementById("button").addEventListener("click", whenClicked, false);
if (typeof global.MutationObserver !== "function") {
// chrome doesn't despatch an event for "DOMAttrModified"
watch.addEventListener("DOMAttrModified", function (evt) {
console.log("Attribute changed", evt.target);
}, false);
} else {
var observer = new global.MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.type === 'attributes') {
console.log("Attribute changed", mutation);
}
});
});
observer.observe(watch, {
attributes: true,
childList: true,
characterData: true,
subtree: true
});
}
}(window));
在jsfiddle
什麼觸發'div'的大小調整? –
如果DIV的寬度僅在窗口大小上發生變化,那麼這裏有什麼問題? –
什麼叫調整大小?現有的插件?或者它有CSS3調整大小:兩個啓用?如果它受到CSS3的約束,請點擊此處 - > [如何檢測CSS3調整大小事件](http://stackoverflow.com/questions/8082729/how-to-detect-css3-resize-events) – Ohgodwhy