js 渐变运动框架
<!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=gb2312" /> <title>无标题文档</title> <style> div{width:100px;height:100px;background:red;filter:alpha(opacity=100);opacity:1;} </style> <script> function getStyle(obj,attr){ return obj.currentStyle ? obj.currentStyle[attr]:getComputedStyle(obj)[attr]; } function startMove(obj,attr,target,fn){ clearInterval(obj.timer); obj.timer = setInterval(function(){ if(attr == 'opacity'){ var iCur = parseInt(parseFloat(getStyle(obj,attr))*100); }else{ var iCur = parseInt(getStyle(obj,attr)); } var speed = (target-iCur)/8; speed = speed >0 ? Math.ceil(speed):Math.floor(speed); if(iCur == target){ clearInterval(obj.timer); fn && fn(); }else{ if(attr == 'opacity'){ obj.style.filter = 'alpha(opacity='+(iCur+speed)+');'; obj.style[attr] = (iCur+speed)/100; }else{ obj.style[attr] = iCur+speed+'px'; } } },30); } window.onload = function(){ var arrInput = document.getElementsByTagName('input')[0]; var arrDiv = document.getElementsByTagName('div')[0]; arrInput.onclick = function(){ startMove(arrDiv,'width',300,function(){ startMove(arrDiv,'height',300,function(){ startMove(arrDiv,'opacity',30); }); }); }; }; </script> </head> <body> <input type='button' value = '开始运动'/> <div></div> </body> </html>