运动框架:
1、在运动开始时,关闭已有定时器。
2、把运动和停止隔开(if ()else())
3、缓冲运动要取整
function startMove(obj,attr,iTarget){
clearInterval(obj.time);
obj.time = setInterval(function(){
var cur = 0;
if (attr == 'opacity') {
cur = Math.round(parseFloat(getStyle(obj,attr))*100);//四舍五入
} else {
cur = parseInt(getStyle(obj,attr));
}
var speed =(iTarget - cur)/6;
speed = speed > 0?Math.ceil(speed):Math.floor(speed);//向上取整,向下取整
if (cur!=iTarget) {
if (attr == 'opacity') {
obj.style.filter = 'alpha(opacity:'+cur + speed+')';
obj.style.opacity = (cur + speed)/100;
} else {
obj.style[attr] = cur + speed + 'px';
}
} else {
clearInterval(obj.time);
}
},30);
}
function getStyle(obj , name) {
if (obj.currentStyle) {//IE下可以使用此方法获取属性
return obj.currentStyle[name];
} else {
return getComputedStyle(obj,false)[name];//火狐和Chrome下用此方法
}
}
透明度:
filter:alpha(opacity:30);//ie下兼容
opacity:0.3//chrome , ff