【CSS3+JQuery】实现万花筒旋转效果

描述:使用css3动画使四个盒子旋转,每隔500ms随机一个div改变成随机背景色, 利用 rgb(255, 255, 255)

在这里插入图片描述

技术点

1.CSS3旋转
2.随机改变颜色

代码实现

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子旋转变色</title>
    <style>
        #wrap {
            width: 500px;
            height: 500px;
            margin: 10px auto;
            position: relative;
        }

        #wrap .position {
            position: absolute;
            left: 50%;
            top: 50%;
        }

        #wrap .box4 {
            width: 400px;
            height: 400px;
            background-color: #DB4A82;
            margin-top: -200px;
            margin-left: -200px;
            animation: clockwise 3s linear infinite;
        }

        #wrap .box3 {
            width: 300px;
            height: 300px;
            background-color: #E1EEBD;
            margin-top: -150px;
            margin-left: -150px;
            animation: anticlockwise 3s linear infinite;
        }

        #wrap .box2 {
            width: 200px;
            height: 200px;
            background-color: #C46DBE;
            margin-top: -100px;
            margin-left: -100px;
            animation: clockwise1 3s linear infinite;
        }

        #wrap .box1 {
            width: 100px;
            height: 100px;
            background-color: #C7CB8E;
            margin-top: -50px;
            margin-left: -50px;
            animation: anticlockwise 3s linear infinite;
        }

        /* 定义动画 */
        @keyframes clockwise {
            0% {
                transform: rotate(0deg);
            }

            50% {
                transform: rotate(180deg);
            }

            100% {
                transform: rotate(360deg);
            }
        }

        @keyframes clockwise1 {
            0% {
                transform: rotate(0deg);
            }

            50% {
                transform: rotate(360deg);
            }

            100% {
                transform: rotate(720deg);
            }
        }

        @keyframes anticlockwise {
            0% {
                transform: rotate(0deg);
            }

            50% {
                transform: rotate(-360deg);
            }

            100% {
                transform: rotate(-720deg);
            }
        }
    </style>
</head>

<body>
    <!-- 1.布局 -->
    <div id="wrap">
        <div class="box4 position">
            <div class="box3 position">
                <div class="box2 position">
                    <div class="box1 position"></div>
                </div>
            </div>
        </div>
    </div>
    <!-- 2.改变背景色 -->
    <script src="./jquery.js"></script>
    <script>
        // 设置背景色
        setInterval(function () {
            // 用随机数取数a,b,c三个值, 都在0-255之间
            var r = Math.floor(Math.random() * 255);
            var g = Math.floor(Math.random() * 255);
            var b = Math.floor(Math.random() * 255);
            var bgColor = "rgb(" + r + "," + g + "," + b + ")";
            // 在1-4之间
            var index = Math.floor(Math.random() * ($('div').length - 1) + 1);
            // console.log(index);
            // console.log(bgColor);
            // box.style.background = bgColor;
            $('div').eq(index).css('background', bgColor);
        }, 500);
    </script>
</body>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值