猿问

麻烦看下我的JS移动动画效果的封装对不对

function startMove(obj,json,fn){
	clearInterval(obj.timer);
	obj.timer=setInterval(function(){
		var flag=true;
		//遍历json
		for(var attr in json)
		{
		//1、取当前值
		var m=0;
		if(attr=="opacity"){
			m=Math.round(parseFloat(getStyle(obj,attr))*100);
			}
		else{m=parseInt(getStyle(obj,attr));}
		//2、算速度
		var speed=(json[attr]-m)/8;
		speed=speed>0?Math.ceil(speed):Math.floor(speed);
		//3、检测停止
		if(m!=json[attr]){
			flag=false;
			if(attr=="opacity"){
				obj.style.filter="alpha(opacity:"+m+speed+")";
				obj.style.opacity=(m+speed)/100;
				}
			else{obj.style[attr]=m+speed+"px";}
		}			}
		if(flag){
			clearInterval(obj.timer);
			if(fn){fn();}
			}
	},30)
}
function getStyle(obj,attr){
	if(obj.currentStyle){
		return obj.currentStyle[attr];
		}
	else{return getComputedStyle(obj,false)[attr];}
}


细嗅蔷薇1
浏览 1305回答 2
2回答

卖火柴的小舒

学会用chrome来调试,看看报错是什么,在多少行,打个短点试试看。慕课有个firebug的课程,去看看咯 !

李晓健

程序这个东西,并不是唯一的写法,对或者不对,你自己去运行就好了。看结果就知道,就算有问题暂时没有发现,但后在用着的时候发现也没有问题,当你发现问题再去解决问题,肯定要比别人直接给你指出来理解的更清楚。
随时随地看视频慕课网APP
我要回答