多物体动画

<!DOCTYPE html>
<html>
<head>
<meta  charset="utf-8" />
<title>多物体动画</title>
<style>
*{margin:0; padding:0; list-style: none;}

#div{
    position: relative;
    left: -200px;
    top: 0;
    width: 200px;
    height: 200px;
    background: orange;
}
#div span{
    position: absolute;
    left: 200px;
    top: 30%;
    width: 20px;
    height: 50px;
    background: red;
}
ul li{
    width: 200px;
    height: 100px;
    margin-bottom: 20px;
    background: #ddd;
}
</style>
</head>

<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
<script>
    window.onload = function(){
        var aLi = document.getElementsByTagName('li');
        for(var i=0; i<aLi.length; i++){
            aLi[i].timer = null; //每个li都加个定时器
            aLi[i].onmouseover = function(){
                startMove(this,400);//this当前的,宽变400
            }
            aLi[i].onmouseout = function(){
                startMove(this,200);
            }
        }
        // var timer;
        function startMove(obj,tagW){  //当前对象,目标宽度

            clearInterval(obj.timer); //关闭定时器
            obj.timer = setInterval(function(){
                //定义速度
                var speed = (tagW - obj.offsetWidth)/8;  //目标宽度-当前宽度
                speed = speed>0?Math.ceil(speed):Math.floor(speed);
                // 缓冲运动速度判断,如果速度大于零向上取整,小于零向下取整
                if(tagW == obj.offsetWidth){
                    clearInterval(obj.timer);
                }else{
                    obj.style.width = obj.offsetWidth + speed +'px';
                }
            },30)

        }
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值