多物体运动框架

时间:2021-04-04 10:13:40
    function startMove(obj,attr,iTart) {//物体,属性,目标值
        clearInterval(obj.time);//注意,首先要清除定时器
        obj.time = setInterval(function () {
            var iCur = 0;//当前非行间样式的值

            if(attr == 'opacity'){//如果是0.3
                iCur = parseInt(parseFloat(getStyle(obj,attr))*100);//为来避免出现小数.
            }else{
                iCur = parseInt(getStyle(obj,attr));//取整
            }

            var spend = (iTart-iCur)/8;//缓冲运动,速度
            spend = spend>0?Math.ceil(spend):Math.floor(spend);//大于0时向上取整,小于0时向下取整
            if(iCur == iTart){//是否到达终点
                clearInterval(obj.time);//到达终点以后,清除定时器
            }else{
                if(attr == 'opacity'){
                    obj.style.filter = 'alpha(opacity:'+(iCur+spend)+')';
                    obj.style.opacity = (iCur+spend)/100;
                    document.getElementById("txt1").value = obj.style.opacity;
                }else{
                    obj.style[attr] = iCur+spend+"px";//到达之前
                }
            }
        },30);

    }