码迷,mamicode.com
首页 > Web开发 > 详细

js运动 多物体运动含Json 但是里面数值不一样

时间:2015-03-11 00:28:41      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

<!doctype html>
<html>
    <head>
        <meta charset = "utf-8">
        <title>未命题</title>
        <style>
            *{margin: 0;
                padding: 0;
            }
            #div1{
                height: 100px;
                width: 100px;
                background: red;
                margin-left: 200px;
                
            }
            
        </style>
        <script>
            window.onload = function ()
            {
                odiv1 = document.getElementById(‘div1‘);
                
                odiv1.onclick = function ()
                {
                    startMove(this,{
                        width:200,
                        height:300
                    }
                    ,10)
                }

                function startMove (obj,json,speed)
                {
                    clearInterval(obj.timer);
                    var icur = 0;

                    obj.timer = setInterval(function(){
                        var ibtn = true;

                        for(var attr in json)
                        {
                            var goal = json[attr];
                            if(attr == ‘opacity‘)
                            {
                                icur = Math.round(css(obj,‘opacity‘)*100);
                            }
                            else
                            {
                                icur = parseInt(css(obj,attr));
                            }


                            if(icur != goal)
                            {
                                ibtn = false;

                                if(attr == ‘opacity‘)
                                {
                                    obj.style.opacity = (iCur + iSpeed) / 100;
                                    obj.style.filter = ‘alpha(opacity=‘+ (iCur + iSpeed) +‘)‘;
                                }
                                else
                                {
                                    obj.style[attr] = icur + speed + ‘px‘;
                                }
                            }    
                        }

                        if(ibtn)
                        {
                            clearInterval(obj.timer);
                        }
                    },30);
                }

                function css(obj,attr)
                {
                    if(obj.currentStyle)
                        return obj.currentStyle[attr];
                    else
                        return getComputedStyle(obj,false)[attr];
                }

            }
        </script>    
    </head>
    <body>
        
        <div id="div1"></div>
        
        
    </body>    

</html>

 

js运动 多物体运动含Json 但是里面数值不一样

标签:

原文地址:http://www.cnblogs.com/mayufo/p/4328762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!