在firefox, webkit中我们可以使用DOMNodeInsertedIntoDocument事件,但这个事件很快变废弃了,虽然浏览器还是很有节操地支持它们,但哪一天不在也很难说。比如说firefox22已经不支持了,IE则始终不支持此事件。

这里有个脚本,可以判定浏览器是否支持变动事件

var mutations = (function (document) {
// (C) WebReflection - Mit Style License
var
type = [
"DOMSubtreeModified",
"DOMNodeInserted",
"DOMNodeRemoved",
"DOMNodeRemovedFromDocument",
"DOMNodeInsertedIntoDocument",
"DOMAttrModified",
"DOMCharacterDataModified"
],
documentElement = document.documentElement,
method = "EventListener",
data = "deleteData",
p = document.createElement("p"),
mutations = {},
i
;
function check(addOrRemove) {
for (i = type.length; i--;) {
p[addOrRemove](type[i], cb, false);
documentElement[addOrRemove](type[i], cb, false);
}
}
function cb(e) {
mutations[e.type] = true;
}
check("add" + method);
documentElement.insertBefore(
p,
documentElement.lastChild
);
p.setAttribute("i", i);
p = p.appendChild(document.createTextNode(i));
data in p && p[data](0, 1);
documentElement.removeChild(p = p.parentNode);
check("remove" + method);
return (p = mutations);
}(document));

《Detect DOM Node Insertions with JavaScript and CSS Animations》这篇文章中,作者提供了一个方法,教我们利用keyframe CSS3开始动画事件来监听节点的插入。


		/* set up the keyframes */
@keyframes nodeInserted {
from { clip: rect(1px, auto, auto, auto); }
to { clip: rect(0px, auto, auto, auto); }
} @-moz-keyframes nodeInserted {
from { clip: rect(1px, auto, auto, auto); }
to { clip: rect(0px, auto, auto, auto); }
} @-webkit-keyframes nodeInserted {
from { clip: rect(1px, auto, auto, auto); }
to { clip: rect(0px, auto, auto, auto); }
} @-ms-keyframes nodeInserted {
from { clip: rect(1px, auto, auto, auto); }
to { clip: rect(0px, auto, auto, auto); }
} @-o-keyframes nodeInserted {
from { clip: rect(1px, auto, auto, auto); }
to { clip: rect(0px, auto, auto, auto); }
} #parentElement > li {
padding: 10px;
background: #FF6A6A;
margin-bottom: 10px;
animation-duration: 0.001s;
-o-animation-duration: 0.001s;
-ms-animation-duration: 0.001s;
-moz-animation-duration: 0.001s;
-webkit-animation-duration: 0.001s;
animation-name: nodeInserted;
-o-animation-name: nodeInserted;
-ms-animation-name: nodeInserted;
-moz-animation-name: nodeInserted;
-webkit-animation-name: nodeInserted;
} <script>
window.onload = function() { var count = 0,
insertListener = function(event){
console.warn("Another node has been inserted! ", event);
if (event.animationName == "nodeInserted") {
event.target.textContent = "Element " + count++ + " has been injected!";
}
} document.addEventListener("animationstart", insertListener, false); // standard + firefox
document.addEventListener("MSAnimationStart", insertListener, false); // IE
document.addEventListener("webkitAnimationStart", insertListener, false); // Chrome + Safari // Insert a new element
setInterval(function(){
document.getElementById("parentElement").appendChild(document.createElement("li"))
}, 2000); };
</script>
var WeakMap = WeakMap || function(){
/*!(C) WebReflection - Mit Style !*/
// WARNING: DO NOT FORGET TO DELETE KEYS WHEN POSSIBLE
// MEMORY GREEDY, UNSAFE, BROKEN SHIM
var w = [].indexOf || function(v){
for(i=this.length;i--&&this[i]!==v;);return i},
i, k, v;
function c(){
k = [];
v = [];
}
function h(o) {
return -1

再看一下它的替代品MutationObserver,发现这厮不支持这功能,它只能监听自身或其后代的变化。因此还是用CSS方法或animationstart,IE6-9只能setInterval了!

posted on
2013-07-15 21:24 
司徒正美 
阅读(...) 
评论(...) 
编辑 
收藏

最新文章

  1. Nodejs之MEAN栈开发(七)---- 用Angular创建单页应用(下)
  2. Enterprise Solution 进销存管理软件 C/S架构,支持64位系统 物流,资金流,信息流全面集成
  3. oracle课堂笔记
  4. 2015弱校联盟(1) - I. Travel
  5. Fork/Join框架之双端队列
  6. 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)
  7. PHP实现浏览历史记录
  8. C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
  9. oracle 10g RAC psu过程
  10. linux下查找某个文件
  11. new/delete 与 malloc/free的区别
  12. Handsontable添加超链接
  13. 《java入门第一季》之面向对象(多态练习)
  14. shell 变量、参数、数组章节笔记
  15. installshield中杀死某一个进程
  16. 【点击项目属性】Target runtime Apache Tomcat v7.0 is not defined
  17. PP助手上传失效
  18. iOS.Debug.Simulator
  19. Spring boot自动设置包依赖,根本不用记,
  20. bzoj 1185

热门文章

  1. 3D场景优化
  2. maven3实战之设置HTTP代理
  3. LA 3971 (二分) Assemble
  4. UVa 11137 (完全背包方案数) Ingenuous Cubrency
  5. 使用C++读写Excel
  6. ubuntu 11.04 源 更新不了,全显示ign、404
  7. I.MX6 AT24Cxx eeprom Linux i2c-gpio
  8. (转)每天一个Linux命令(4): mkdir
  9. 【英语】Bingo口语笔记(45) - Pass系列
  10. SQL利用Case When Then多条件判断