1. 程式人生 > >JavaScript之鏈式運動

JavaScript之鏈式運動

function getStyle(obj, name) { if(obj.currentStyle) { return obj.currentStyle[name]; }else { return getComputedStyle(obj, false)[name]; } } //var alpha = 30; function startMove(obj, arr, iTarget,fn) { clearInterval(obj.timer); obj.timer=setInterval(function
(){
var cur=0; if(arr=='opacity') { cur=Math.round(parseFloat(getStyle(obj, arr))*100); }else { cur=parseInt(getStyle(obj, arr)); } 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(arr=='opacity') { obj.style.filter='alpha(opcity:'+(cur+speed)+')'; obj.style.opacity=(cur+speed)/100; }else { obj.style[arr]=cur+speed+'px'
; } } }, 30); } window.onload=function() { var div1 = document.getElementById('div1'); div1.onclick=function() { startMove(div1,'height',300,function() { startMove(div1,'width',300,function(){ startMove(div1,'opacity',30); }) }); } }