能不能幫我看看問題在哪里呢? 麻煩了
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
function startMove(obj,json,fn){
obj.timer=null;
clearInterval(obj.timer);
obj.timer=setInterval(function(){
for(var attr in json){
var flag=true;
var speed=0;
var icur;
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(attr=="opacity"){
? ? ? ?obj.style.filter="alpha(opacity:"+(icur+speed)+")";
? ? ? ?obj.style.opacity=(icur+speed)/100;
? ? ? ?}else{
? ? ? ?obj.style[attr]=icur+speed+"px";
? ? ? ?}
? ? ? ?if(icur!=json[attr]){
? ? ? ? flag=false; ? ? ? ?
? ? ? ?}
? ? ? ?if(flag){
? ? ? ? clearInterval(obj.timer);
? ? ? ?if(fn){
? ? ? ?fn();
? ? ? ?}
? ? ? ?} ?
? ? ? ? }
},30)
}
2016-10-21
function getStyle(obj,attr){
if(obj.currentStyle){//Ie瀏覽器
return obj.currentStyle[attr];
}
else{
return getComputedStyle(obj,false)[attr];//谷歌瀏覽器
}
}
//調(diào)用示例:move(div1,{width:201,height:200,opacity:10},function(){alert('ddadasd');});
function move(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var flag=true;
for(var attr in json){?
var icur=0;
if(attr=='opacity'){
icur=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
icur=parseInt(getStyle(obj,attr));
}
var speed=(json[attr]-icur)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);//取整
? if(icur!=json[attr]){//判斷是否完成
? if(attr=='opacity'){//判斷透明度
? obj.style.filter='alpha(opacity:'+(icur+speed)+')';//IE透明度
? obj.style.opacity=(icur+speed)/100;//谷歌透明度
? }else{
? obj.style[attr]=icur+speed+"px";
? }
? flag=false;
? }
?
?
?
? }
? if(flag){
? clearInterval(obj.timer);//清楚定時器
? if(fn){
? fn();//執(zhí)行回掉函數(shù)
? }
? }
? },30);
}
2016-10-17
var flag=true;放在循環(huán)外定時器內(nèi);
?if(flag){
? ? ? ? clearInterval(obj.timer);
? ? ? ?if(fn){
? ? ? ?fn();
? ? ? ?}
? ? ? ?}
放在循環(huán)外