function getStyle(obj,attr) {
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
}
function startMove(obj,attr,target) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
// 返回的字符串,需转换成number类型
// var curAttr = parseInt(getStyle(obj,attr));
// var curAttr = (attr == 'opacity') ? parseFloat(getStyle(obj,attr))*100 : parseInt(getStyle(obj,attr));
var curAttr = (attr == 'opacity') ? parseInt(parseFloat(getStyle(obj,attr))*100) : parseInt(getStyle(obj,attr));
var speed = (target - curAttr)/8;
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if (curAttr == target) { //是否到达终点
clearInterval(obj.timer);
} else {
if (attr == 'opacity') {
curAttr += speed;
obj.style.opacity = (curAttr + speed)/100;
obj.style.filter = 'alpha(opacity:' + (curAttr + speed) + ')';
document.getElementById('txt').value = obj.style.opacity;
} else {
obj.style[attr] = curAttr + speed + 'px';
}
}
},300);
}
网友评论