简易运动函数
function StarMove(obj,attr,iTarget) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var iCur =parseInt(getStyle(obj, attr));
var iSpeed = (iTarget - iCur)/6;
iSpeed = iSpeed > 0? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(iCur == iTarget) {
clearInterval(obj.timer);
}else {
obj.style[attr] = iCur + iSpeed +'px';
}
},30)
}
function getStyle(obj, attr) {
if(obj.currentStyle) {
return obj.currentStyle[attr];
}else {
return getComputedStyle(obj, false)[attr];
}
}
多物体运动
for(var i = 0; i < oDiv.length; i++) {
oDiv[i].timer = null;
oDiv[i].onmouseover = function(){
StarMove(this, 'width',300);
}
oDiv[i].onmouseout = function(){
StarMove(this,'width', 100);
}
}
网友评论