js perfect motion framework

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style>
div{width:100px;height:100px;background:red;}
</style>
<script>
    function getStyle(obj,attr){
        return obj.currentStyle ? obj.currentStyle[attr]:getComputedStyle(obj)[attr];
    }
    function perfectStartMove(obj,json){
        clearInterval(obj.timer);
        obj.timer = setInterval(function(){
            var bStop = true;
            for(var attr in json){
                if(attr == 'opacity'){
                    var iCur = parseInt(parseFloat(getStyle(obj,attr))*100);
                }else{
                    var iCur = parseInt(getStyle(obj,attr));
                }
                var speed = (json[attr]-iCur)/8;
                speed = speed >0 ? Math.ceil(speed):Math.floor(speed);
                if(iCur != json[attr]){
                    bStop =  false ;
                }
                if(attr == 'opacity'){
                    obj.style.filter = 'alpha:(opacity='+(iCur+speed)+')';
                    obj.style[attr] = (iCur+speed)/100;
                }else{
                    obj.style[attr] = iCur+speed+'px';
                }    
            }    
            if(bStop){
                clearInterval(obj.timer);
            }
            
        },30);                
    }        
    window.onload = function(){
        var arrInput = document.getElementsByTagName('input')[0];
        var arrDiv = document.getElementsByTagName('div')[0];
        var onOff = true;
        arrInput.onclick = function(){
            if(onOff){
                perfectStartMove(arrDiv,{width:300,height:300,opacity:30});
            }else{
                perfectStartMove(arrDiv,{width:100,height:100,opacity:100});
            }
            onOff =  ! onOff;
        };
    };
    
</script>
</head>

<body>
<input type='button' value = '开始运动'/>
<div></div>
</body>
</html>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325491734&siteId=291194637