<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
.box{width:100px;height:100px;background:red; transition:1s width,2s height;}
</style>
</head>
<body>
<div class="box" id="box"></div>
<script>
var oBox=document.getElementById("box");
oBox.onclick=function()
{
this.style.width=this.offsetWidth+100+"px";
this.style.height=this.offsetHeight+100+"px";
};
addEnd(oBox,function(){//----》问题1:教程里面说是有两次结束transition,所以会弹出两次,但是js不是按照顺序结构执行代码的吗?这里函数只调用了一次啊?
alert("end");
});
function addEnd(obj,fn)
{
obj.addEventListener('WebkitTransitionEnd',fn,false);//--》问题2:这里为什么不需要加判断是否能读取WebkitTransitionEnd,要是非chrome浏览器读取不了WebkitTransitionEnd,下面的代码应该不会执行才对啊,但是ie,ff都能执行,这是什么原因?
obj.addEventListener('transitionend',fn,false);
}
</script>
</body>
</html>
问题1:在你点击了div后,执行onclick函数,div宽、高各增加100px;这里宽和高算两次变形。onclick确实是只调用了一次它的点击事件。但是你在下面的代码监听了transitionend事件类型,两次变形结束触发两次transitionend事件,所以弹出两次弹框,没毛病。。。
问题2:addEventListener只是注册了事件。如果这个事件没被触发,就不会执行里面的函数。也就是说你即使这样
WebkitTransitionEnd是兼容以前的老浏览器。现代浏览器都是使用transitionend。WebkitTransitionEnd只有在Chrome 1.0, Android 2.1 与 WebKit 3.2上才存在,所以你在现在的浏览器上使用时,
obj.addEventListener('WebkitTransitionEnd',fn,false)//这个事件不会触发
。真正触发的都是transitionend事件。