js 简单的动画过渡效果

js 利用定时器将页面上的盒子颜色逐个改变颜色

效果图:

HTML代码:

  <div class="box">
        <div class="box_child"></div>
        <div class="box_child"></div>
        <div class="box_child"></div>
        <div class="box_child"></div>
        <div class="box_child"></div>
    </div>

css代码:

 <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 100%;
            display: flex;
        }

        .box_child {
            width: 200px;
            height: 100px;
            margin: 20px;
            background-color: red;
        }
    </style>

js代码:

 <script>
        
        let times = setInterval(fun, 1000); // 设置永久定时器  
        let index = 0 // 当前过渡到的元素数量
        // 获取父元素
        let box = document.getElementsByClassName('box')[0];
        let box_child = document.getElementsByClassName('box_child');
        function fun() {
            // 如果动画执行完毕就重新开始
            if (index == 5) {
                index = 0;
            } else {
                // 否则动画就继续执行
                index++;
            }
            for (let i = 0; i < box_child.length; i++) {
                if (index > i) {
                    box_child[i].style.backgroundColor = 'blue'
                } else {
                    box_child[i].style.backgroundColor = ''
                }
            }
        }
    </script>

整体代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动画过渡</title>
    <script src="jquery-3.4.1.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 100%;
            display: flex;
        }

        .box_child {
            width: 200px;
            height: 100px;
            margin: 20px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="box_child"></div>
        <div class="box_child"></div>
        <div class="box_child"></div>
        <div class="box_child"></div>
        <div class="box_child"></div>
    </div>
    <script>
        
        let times = setInterval(fun, 1000); // 设置永久定时器  
        let index = 0 // 当前过渡到的元素数量
        // 获取父元素
        let box = document.getElementsByClassName('box')[0];
        let box_child = document.getElementsByClassName('box_child');
        function fun() {
            // 如果动画执行完毕就重新开始
            if (index == 5) {
                index = 0;
            } else {
                // 否则动画就继续执行
                index++;
            }
            for (let i = 0; i < box_child.length; i++) {
                if (index > i) {
                    box_child[i].style.backgroundColor = 'blue'
                } else {
                    box_child[i].style.backgroundColor = ''
                }
            }
        }
    </script>
</body>

</html>

运行原理:设置定时器来改变盒子元素的背景颜色,每次执行定时器index的值都会增加,index的值如果大于当次循环的值,就让盒子元素改变背景颜色,如果index的值等于当前盒子元素的数量时,让它归零,并让盒子元素的背景颜色变会之前的颜色。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值