随机抽奖

效果图

代码:

<!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>
    <style>
      .wrapper {
        width: 840px;
        height: 420px;
        background: url(./bg01.jpg) no-repeat center / cover;
        padding: 100px 250px;
        box-sizing: border-box;
      }

      .wrapper span {
        color: #b10e0d;
      }
    </style>
  </head>

  <body>
    <div class="wrapper">
      <strong>年会抽奖</strong>
      <h1>一等奖:<span class="one">???</span></h1>
      <h3>二等奖:<span class="two">???</span></h3>
      <h5>三等奖:<span class="three">???</span></h5>
    </div>

    <script>
      //1.创建一个数组,用于存放人员名单
      const arr = ["牵丝戏安", "古力娜扎", "迪丽热巴", "马儿扎哈", "霜叶游侠"];
      //2.利用随机数选取数组中的名字
      const random1 = Math.floor(Math.random() * arr.length);
      // console.log(arr[random]);
      //3.把名字放入对应的盒子里  span
      // 一等奖
      const one = document.querySelector(".one");
      one.innerText = arr[random1];

      //4.不允许重复抽奖,要把刚才选出来的名字从数组中删除
      arr.splice(random1, 1);
      //二等奖
      const random2 = Math.floor(Math.random() * arr.length);
      // console.log(arr[random2]);
      const two = document.querySelector(".two");
      two.innerText = arr[random2];

      arr.splice(random2, 1);
      // 三等奖
      const random3 = Math.floor(Math.random() * arr.length);
      // console.log(arr[random]);
      const three = document.querySelector(".three");
      three.innerText = arr[random3];
    </script>
  </body>
</html>

背景图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值