js点名器

总代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    *{
      margin: 0px;
      padding: 0px;
      background-color: cornsilk;
    }
    .d1{
      margin: auto;
      margin-top: 150px;
      /*border: 1px solid black;*/
      border-radius: 60px;
      background-color: springgreen;
      width: 800px;
      height: 400px;
      position: relative;
    }
    .d1:hover{
      box-shadow: 0px 0px 65px 30px #00ff7f;
    }
    .d1 > h2{
      width: 300px;
      height: 80px;
      text-align: center;
      line-height: 80px;
      color: cornsilk;
      position: absolute;
      font-size: 55px;
      left: 260px;
      background-color: springgreen;
      /*border: 1px solid black;*/
    }
    .d1 > #hh{
      width: 200px;
      height: 200px;
      position: absolute;
      left: 310px;
      top: 90px;
      font-size: 20px;
      letter-spacing: 20px;
      text-align: center;
      line-height: 200px;
      border-radius: 200px;
    }
    #ks{
      width: 200px;
      height: 40px;
      position: absolute;
      left: 310px;
      top: 320px;
    }
  </style>

</head>
<body>
  <div class="d1">
    <h2>是谁呢??</h2>
    <div id = "hh">???</div>
    <button id="ks">开始</button>
  </div>

  <script>
    window.addEventListener("load",function () {

<!--    获取标签-->
    let name = document.getElementById("hh");
    let button = document.getElementById("ks");
    let div = document.getElementsByClassName("d1");


  // 创建数组
    let arrName = ["司岱钰","张晨林","王志超","胡钰乾","殷俊峰","李佳和","王泽烔"];

  // 创建随机数
    let time = null;
    let hh = 0;
    let fn = 0;
    button.onclick = function (){
      if (hh == 1) {
        time = window.clearInterval(time);
        button.innerHTML = "开始";
        hh = 0;
      }else{
        hh = 1;
        time = self.setInterval(function () {
          // 创建随机数
          name.innerHTML = arrName[Math.floor(Math.random()*arrName.length)]
        },30);
        console.log(fn++)
        button.innerHTML = "结束";
      }
    }
    })
  </script>
</body>
</html>
1.html部分
  <div class="d1">
    <h2>是谁呢??</h2>
    <div id = "hh">???</div>
    <button id="ks">开始</button>
  </div>
2.css部分
  <style>
    *{
      margin: 0px;
      padding: 0px;
      background-color: cornsilk;
    }
    .d1{
      margin: auto;
      margin-top: 150px;
      /*border: 1px solid black;*/
      border-radius: 60px;
      background-color: springgreen;
      width: 800px;
      height: 400px;
      position: relative;
    }
    .d1:hover{
      box-shadow: 0px 0px 65px 30px #00ff7f;
    }
    .d1 > h2{
      width: 300px;
      height: 80px;
      text-align: center;
      line-height: 80px;
      color: cornsilk;
      position: absolute;
      font-size: 55px;
      left: 260px;
      background-color: springgreen;
      /*border: 1px solid black;*/
    }
    .d1 > #hh{
      width: 200px;
      height: 200px;
      position: absolute;
      left: 310px;
      top: 90px;
      font-size: 20px;
      letter-spacing: 20px;
      text-align: center;
      line-height: 200px;
      border-radius: 200px;
    }
    #ks{
      width: 200px;
      height: 40px;
      position: absolute;
      left: 310px;
      top: 320px;
    }
  </style>
 3.js部分代码解释

document.getElementById------是用来获取id名称的

document.getElementsByclassName-------是用来获取类(class)名的

定义的变量名 hh 本质就是true 、false 用 0、1代替了(用于做判断)

Math.random()生成0到1之间的随机数

Math.floor()向下取整

  <script>
    window.addEventListener("load",function () {

<!--    获取标签-->
    let name = document.getElementById("hh");
    let button = document.getElementById("ks");
    let div = document.getElementsByClassName("d1");


  // 创建数组
    let arrName = ["司岱钰","张晨林","王志超","胡钰乾","殷俊峰","李佳和","王泽烔"];

  // 创建随机数
    let time = null;
    let hh = 0;
    let fn = 0;
    button.onclick = function (){
      if (hh == 1) {
        time = window.clearInterval(time);
        button.innerHTML = "开始";
        hh = 0;
      }else{
        hh = 1;
        time = self.setInterval(function () {
          // 创建随机数
          name.innerHTML = arrName[Math.floor(Math.random()*arrName.length)]
        },30);
        console.log(fn++)
        button.innerHTML = "结束";
      }
    }
    })
  </script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值