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

js 多物体运动框架

时间:2017-05-14 14:22:55      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:ack   for循环   ceil   over   elements   body   步骤   var   调用   

多物体运动框架
例子:多个Div,鼠标移入biankuan
单定时器,存在问题
每个Div一个定时器
总结:
参数问题:
当运动当前的div的时候可以传参数
onStart(obj,tag);
obj为当前运动的div 调用时用this
tag表示运动的终点距离
开一个定时器(当三个div一起运动时会卡)
所以利用for循环开三个定时器
步骤:
1.设置定时器 var timer=null;
2.关闭定时器
clearInterval(obj.timer);
3.开启定时器:
obj.timer=setInterval(function(){},30);
4.缓冲时设置速度 并且要取整
5.判断当前位置与目标位置是否相等

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
div{width:200px;
height:50px;
background:#F00;
margin:10px;
}
</style>
<script>
window.onload=function(){
    var aDiv=document.getElementsByTagName(‘div‘);
    for(var i=0;i<aDiv.length;i++){
        aDiv[i].timer=null;
        aDiv[i].onmouseover=function(){
             onStart(this,400);
            };
            aDiv[i].onmouseout=function(){
             onStart(this,100);
            };
            
        }
    }
    //var timer=null;
function onStart(obj,tag){
    clearInterval(obj.timer);
    obj.timer=setInterval(function(){
        var speed=(tag-obj.offsetWidth)/6;
        speed=speed>0?Math.ceil(speed):Math.floor(speed);
        if(obj.offsetWidth==tag){
            clearInterval(obj.timer);
            }else{
                obj.style.width=obj.offsetWidth+speed+‘px‘;
                }
        
        },30);
    
    }
</script>
</head>



<body>

<div></div>
<div></div>
<div></div>
</body>
</html>

 

js 多物体运动框架

标签:ack   for循环   ceil   over   elements   body   步骤   var   调用   

原文地址:http://www.cnblogs.com/zwn-lucky7/p/6852394.html

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