完美运动框架 当不用onnmouseout时,鼠标离开再放上去时会把链式动画重新走一变,这是bug吗
<html>
<head>
<title>move</title>
<meta charset="utf-8" />
<style>
*{margin:0;
padding:0;}
div{width:200px;
height:200px;
opacity:0.3;
filter:alpha(opacity:30);
background:red;}
</style>
<script type="text/javascript">
//完美运动框架
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
function startMove(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var flag=true;
for(var attr in json){
var icur=0;
var speed=0;
if(attr=='opacity'){
icur=Math.round(parseFloat(getStyle(obj,attr))*100)
}else{
icur=parseInt(getStyle(obj,attr))
}
speed=(json[attr]-icur)/10;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(json[attr]!=icur){
flag=false;
}
if(attr=='opacity'){
obj.style.filter='alpha(opacity:'+(icur+speed)+')';
obj.style.opacity=(icur+speed)/100;
}else{
obj.style[attr]=icur+speed+'px';
}
}
if(flag==true){
clearInterval(obj.timer);
if(fn){
fn();
}
}
},30)
}
</script>
<script type="text/javascript">
window.onload=function(){
var div=document.getElementById('div');
div.onmouseover=function(){
startMove(div,{width:201,height:400,opacity:50},function(){startMove(div,{width:100})})
}
// div.onmouseout=function(){
// startMove(div,{opacity:50},function(){startMove(div,{width:200,height:200})})
// }
}
</script>
</head>
<body>
<div id="div"></div>
</body>
</html>