<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><style type="text/css">*{margin:0;padding:0}div{width:200px;height:200px;background:#9F0;}</style></head><body><div id="div1"></div><script type="text/javascript">window.onload=function(){ var div1=document.getElementById('div1'); div1.onmouseover=function(){ startMove(div1,{'width':300,'opacity':30}); } div1.onmouseout=function(){ startMove(div1,{'width':200,'opacity':100}); }}function startMove(obj,Json,fn){ var flag=true; clearInterval(obj.timer); obj.timer=setInterval(function(){ for(var attr in Json){ now=0; if(attr=='opacity'){ now=parseFloat(getStyle(obj,attr))*100; } else{ now=parseInt(getStyle(obj,attr)); } speed=(Json[attr]-now)/10; speed=speed>0?Math.ceil(speed):Math.floor(speed); if(Json[attr]!=now){ flag=false; } if(attr=='opacity'){ obj.style.opacity=(now+speed)/100; obj.style.filter='alpha(opacity:'+(now+speed)+')'; } else{ obj.style[attr]=now+speed+'px'; } if(flag){ clearInterval(obj.timer); if(fn){ fn(); } } }},10)}function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; } else{ return getComputedStyle(obj,false)[attr]; }}</script></body></html>
添加回答
举报
0/150
提交
取消