冰中何人知
2016-01-10 20:16
<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>链式运动</title> <style> div{width:200px;height:200px;background-color:yellow;margin:10px;float:left;} </style> <script type="text/javascript"> window.onload=function() { var oDiv1=document.getElementById('div1'); oDiv1.onmouseover=function() { startMove(this,'height',400,function() { startMove(oDiv1,'opacity',50); }); }; oDiv1.onmouseout=function() { startMove(this,'height',200); }; } function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr];} else{ return getComputedStyle(obj,false)[attr];} } function startMove(obj,attr,iTarget,fn) { clearInterval(obj.timer); obj.timer=setInterval(function(){ var cur=0; if(attr=='opacity') { cur=Math.round(parseFloat(getStyle(obj,attr))*100); } else { cur=parseInt(getStyle(obj,attr))}; var speed=(iTarget-cur)/6; speed=speed>0?Math.ceil(speed):Math.floor(speed); if (cur==iTarget) { clearInterval(obj.timer); if(fn){ fn(); } } else { if(attr=='opacity') { obj.style.filter='alpha(opacity:'+(cur+speed)+')'; obj.style.opacity=(cur+speed)/100; } else{obj.style[attr]=cur+speed+'px';} } },30); } </script> </head> <body> <div id=div1></div> </body> </html>
currentStyle的结果在)到1之间,所以cur要x100;
使用50是为了和width和height保持一致。来计算speed
JS动画效果
113925 学习 · 1443 问题
相似问题