javascript運動框架

javascript運動框架:javascript

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Author" content="FengYu">
    <title>運動框架</title>
    <style>
        *{margin:0;padding:0;font-family: Microsoft YaHei,serif;}
	    li{list-style: none;}
        #box{
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 100px;
            background: pink;
            opacity: 1;
        }
    </style>
</head>
<body>
<div id="box"></div>
    <script>
        tMove(box,{
            width: 500,
            height: 500,
            opacity:0.1
        },2000,function(){
            box.style.background = "#000";
        });
        function tMove(obj, json, time, callBack) {
            window.requestAnimationFrame = window.requestAnimationFrame||function(fn){return setTimeout(fn,1000/60)};
            window.cancelAnimationFrame = window.cancelAnimationFrame||clearTimeout;
            var getStyle = obj.currentStyle || getComputedStyle(obj);
            var start = {};var distents = {};
            for(var key in json){
                start[key] = parseFloat(getStyle[key]);
                distents[key] = json[key] - start[key];
                if(!distents[key]){
                    delete start[key];
                    delete json[key]
                }
            }
            var startDate = new Date();
            fn()
            function fn(){
                var pro = (new Date() - startDate)/time;
                if(pro>=1){
                    pro = 1;
                    cancelAnimationFrame(fn);
                   callBack && callBack()
                }else{
                    requestAnimationFrame(fn)
                };
                for(var key in start){
                    if(key == "opacity"){
                       var value = pro*distents[key] + start[key];
                        obj.style[key] = value;
                        obj.style.filter = "alpha(opacity="+ value*100 +")"
                    }
                    obj.style[key] = pro*distents[key] + start[key] +"px"
                };

            }

        }
    </script>
<script>
</script>
</body>
</html>
相關文章
相關標籤/搜索