Created
July 23, 2014 09:13
-
-
Save cycold/cd170766cf5021c527a2 to your computer and use it in GitHub Desktop.
回调函数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// function startMove(obj,attr,iTarget,fnEnd){ //多个参数使用json格式 | |
function startMove(obj,json,fnEnd){ | |
clearInterval(obj.timer); | |
obj.timer = setInterval(function(){ | |
var flag = true; //标志位 假设所有的值都到位了 | |
for(var attr in json){ | |
var cur = 0; | |
//对透明度做一个判断 | |
if(attr == "opacity"){ | |
// var cur = parseFloat(getStyle(obj,attr))*100; //使用paseFloat保留小数 同时习惯使用100的透明度 | |
cur = Math.round(parseFloat(getStyle(obj,attr))*100); //使用paseFloat保留小数 同时习惯使用100的透明度 使用round使小数到达整数 | |
}else{ | |
cur = parseInt(getStyle(obj,attr)); //注意如果对于透明度的小数 parseInt会去掉小数 | |
} | |
var speed = (json[attr] - cur)/7; | |
speed = speed>0?Math.ceil(speed):Math.floor(speed); | |
if (cur!=json[attr]){ //如果发现有属性值还没有到 | |
flag = false; | |
} | |
//对于透明度 | |
if(attr == "opacity"){ | |
obj.style.filter = "alpha(opacity:"+ (cur + speed) +")"; //注意加括号防止字符串的默认连接 | |
obj.style.opacity = (cur + speed)/100; | |
}else{ | |
obj.style[attr] = cur + speed + "px"; //透明度是没有px单位的 | |
} | |
} | |
//当整个循环结束后,flag变为true 就说明所有的属性值都到了,这时就可以关闭定时器了 | |
if(flag){ | |
clearInterval(obj.timer); | |
if (fnEnd) { | |
fnEnd(); | |
} | |
} | |
},30); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment