js+css+html 抽奖页面

 <div class="container">
        <div class="block1 block">小米笔记本</div>
        <div class="block2 block">iPhoneX</div>
        <div class="block3 block">PS4游戏机</div>
        <div class="block4 block">谢谢参与</div>
        <div class="block5 block">开始抽奖</div>
        <div class="block6 block">索尼电视机</div>
        <div class="block7 block">西门子冰箱</div>
        <div class="block8 block">老板抽油烟机</div>
        <div class="block9 block">空气净化机</div>
    </div>
<style>
        .container {
            width: 600px;
            height: 600px;
            border: 1px solid;
            display: flex;
            flex-wrap: wrap;
            margin: 100px auto;
        }

        .block {
            width: 33.33%;
            height: 33.33%;
            outline: 1px solid black;
            text-align: center;
            line-height: 200px;
            font-size: 26px;
        }

        .block5 {
            background-color: skyblue;
            cursor: pointer;
        }
    </style>
<script>
        let blocks = document.getElementsByClassName("block");
        // 数组存放的是盒子的下标,按照顺时针的顺序
        let arr = [0, 1, 2, 5, 8, 7, 6, 3], i = 0, count = 0, stopTimer;
        let rand = Math.floor(Math.random() * 8 + 50); // 给出一个停止计时器的随机数 count的值等于这个随机数时停止计时器
        let around = function () {
            // 将其他盒子都变为白色
            for (let j = 0; j < arr.length; j++) {
                blocks[arr[j]].style.background = "white";
            }
            // 这里blocks的取值为 blocks[0] blocks[1] blocks[2] blocks[5] blocks[8] blocks[7] blocks[6] blocks[3]
            // 将当前arr[i]值所对应的盒子修改为粉色
            blocks[arr[i]].style.background = "pink";
            i++; 
            // 重制i的值
            if (i === arr.length) {
                i = 0;
            }
            count++; // count是一个计数器 根据count的值来调整速度
            // 下面的4个if根据count的值来关闭计时器和重启计时器 改变计时器的时间间隔
            if (count === 5 || count === 45) {
                clearInterval(stopTimer);
                stopTimer = setInterval(around, 200);
            }
            if (count === 10 || count === 35) {
                clearInterval(stopTimer);
                stopTimer = setInterval(around, 100);
            }
            if (count === 15) {
                clearInterval(stopTimer);
                stopTimer = setInterval(around, 50);
            }
            // 当等于上面的随机数时,停止计时器
            if (count === rand) {
                clearInterval(stopTimer);
            }
        }
        // 给开始按钮绑定点击事件 点击后执行 around 
        let start = function () {
            blocks[4].removeEventListener("click", start); // 当用户点击了开始按钮后 必须要移除该事件 防止用户重复点击
            stopTimer = setInterval(around, 300);
        }
        blocks[4].addEventListener("click", start);
    </script>

在这里插入图片描述

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
抽奖页面的实现需要前端技术,包括HTMLCSS和JavaScript。下面是一个简单的抽奖页面示例,代码如下: HTML代码: ```html <!DOCTYPE html> <html> <head> <title>抽奖页面</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <h1>抽奖页面</h1> <div class="lottery"> <div class="lottery-box"> <div class="lottery-cards"> <div class="card"> <img src="images/card1.png"> </div> <div class="card"> <img src="images/card2.png"> </div> <div class="card"> <img src="images/card3.png"> </div> <div class="card"> <img src="images/card4.png"> </div> <div class="card"> <img src="images/card5.png"> </div> </div> <button class="btn" id="btn-start">开始抽奖</button> </div> </div> </div> <script type="text/javascript" src="script.js"></script> </body> </html> ``` CSS代码: ```css .container { max-width: 960px; margin: 0 auto; padding: 20px; } .lottery { margin-top: 50px; text-align: center; } .lottery-box { position: relative; display: inline-block; } .lottery-cards { position: absolute; top: 0; left: 0; right: 0; bottom: 0; display: flex; align-items: center; justify-content: center; flex-wrap: wrap; } .card { width: 100px; height: 100px; margin: 10px; background-color: #fff; border: 1px solid #ddd; border-radius: 5px; display: flex; align-items: center; justify-content: center; } .card img { max-width: 100%; max-height: 100%; } .btn { display: block; margin: 50px auto; padding: 10px 20px; background-color: #f00; color: #fff; border: none; border-radius: 5px; font-size: 24px; cursor: pointer; } .btn:hover { background-color: #f66; } ``` JavaScript代码: ```javascript var btnStart = document.getElementById('btn-start'); var lotteryCards = document.querySelectorAll('.card'); btnStart.addEventListener('click', function() { // 随机选择一个卡片 var index = Math.floor(Math.random() * lotteryCards.length); // 添加选中样式 lotteryCards[index].classList.add('selected'); }); ``` 以上代码实现了一个简单的抽奖页面,其中包含了五张卡片,点击“开始抽奖”按钮后,页面随机选中一张卡片,并添加选中样式。你可以根据自己的需求修改代码,实现更为复杂的抽奖页面
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值