慕田峪5928195
2016-10-17 12:23
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)
}
function getStyle(obj,attr){
if(obj.currentStyle){//Ie浏览器
return obj.currentStyle[attr];
}
else{
return getComputedStyle(obj,false)[attr];//谷歌浏览器
}
}
//调用示例: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();//执行回掉函数
}
}
},30);
}
var flag=true;放在循环外定时器内;
if(flag){
clearInterval(obj.timer);
if(fn){
fn();
}
}
放在循环外
JS动画效果
113923 学习 · 1443 问题
相似问题
回答 1
回答 1
回答 2
回答 1
回答 6