HTML:随机点名网页

效果在线展示(可直接fork后修改JS里面姓名列表自用):

网页源码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>随机点名</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #60AEEF;
    }
    .container {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
      z-index: 1;
    }
    h1 {
      font-size: 4rem;
      color: #fff;
      text-shadow: 0 0 10px rgba(0,0,0,0.5);
      margin-bottom: 1rem;
    }
    #name-display {
      font-size: 3rem;
      color: #fff;
      text-shadow: 0 0 10px rgba(0,0,0,0.5);
      margin-bottom: 2rem;
    }
    button {
      font-size: 2rem;
      padding: 1rem 2rem;
      background-color: transparent;
      border: 2px solid #fff;
      color: #fff;
      cursor: pointer;
      transition: all 0.3s ease-in-out;
    }
    button:hover {
      background-color: #fff;
      color: #000;
    }
    .result {
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      background-color: blue;
      color: white;
      font-weight: bold;
      padding: 1rem 2rem;
      font-size: 3rem;
      display: none;
    } 
    
    /* 自适应 */
    @media only screen and (max-width: 768px) {
      .result {
        font-size: 2rem;
        padding: 0.5rem 1rem;
      }
    }
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>随机点名器</h1>
    <p id="name-display">准备好了吗?</p>
    <button id="start-button">开始</button>
    <div class="result"></div>
  </div>
  <script>
    document.addEventListener('DOMContentLoaded', function() {
      var names = ["张三", "李四", "王五", "赵六", "刘七"];
      var nameDisplay = document.getElementById("name-display");
      var startButton = document.getElementById("start-button");
      var resultBox = document.querySelector(".result");
      var timer, index;

      startButton.addEventListener("click", function() {
        index = 0; // 初始化名字索引
        nameDisplay.innerText = ""; // 清空名字显示区域
        clearInterval(timer); // 清除之前的定时器

        // 设置一个新的定时器循环遍历所有名字
        timer = setInterval(function() {
          if (index >= names.length) { // 如果已经循环到最后一个名字
            clearInterval(timer); // 清除定时器
            var selectedName = names[Math.floor(Math.random() * names.length)]; // 选择随机名字
            nameDisplay.innerText = selectedName; // 将随机名字显示在名字显示区域中
          } else {
            nameDisplay.innerText = names[index]; // 将当前索引对应的名字显示出来
            index++; // 名字索引增加一
          }
        }, 50);
      });
    });
  </script>
</body>
</html>

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值