[英]Listening browser reflow event
我正在嘗試收聽瀏覽器回流事件,以了解代碼的哪些部分是最昂貴的。 當必須(重新)繪制到屏幕上時,例如當將新元素添加到DOM時,會發生回流。
有沒有辦法在例如/用Javascript中監聽這些事件,以便進一步分析?
我認為使用DOM MutationObserver類的解決方案。 正如文件所指出的那樣:
它被設計為DOM3 Events規范中定義的Mutation Events的替代品。 Api Docs
該網站上的示例非常自我解釋
// select the target node
var target = document.querySelector('#some-id');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation.type);
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
// later, you can stop observing
observer.disconnect();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.