模拟双色球开奖HTML源码

题目描述

使用html+js+css完成模拟双色球开奖功能,点击生成红色球随机数,6个红色球不停的刷新数字,直到点击停止生成红球出现结果,点击生成蓝色随机数按钮,一个蓝色球不停的刷新数字,直到点击停止生成蓝球

实现代码

<!DOCTYPE html>
<html>
  <head>
    <title>双色球开奖(6+1)</title>
    <style>
      main {
        margin: 0 auto;
        max-width: 800px;
        text-align: center;
        padding-top: 50px;
      }

      h1 {
        font-size: 36px;
        font-weight: bold;
      }

      .balls {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 20px;
      }

      .red-balls {
        display: flex;
        margin-right: 10px;
      }

      .ball {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        font-size: 24px;
        font-weight: bold;
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .red {
        background-color: #f44336;
        border: 2px solid #f44336;
      }

      .blue {
        background-color: #2196f3;
        border: 2px solid #2196f3;
      }

      button {
        margin-top: 20px;
        padding: 10px 20px;
        font-size: 18px;
        border: none;
        border-radius: 5px;
        background-color: #4caf50;
        color: #fff;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <main>
      <h1>双色球开奖(6+1)</h1>
      <div class="balls">
        <div class="red-balls">
          <div class="ball red"></div>
          <div class="ball red"></div>
          <div class="ball red"></div>
          <div class="ball red"></div>
          <div class="ball red"></div>
          <div class="ball red"></div>
        </div>
        <div class="ball blue"></div>
      </div>
      <div class="buttons">
        <button id="start-red">开始生成红球</button>
        <button id="stop-red">停止生成红球</button>
        <button id="start-blue">开始生成蓝球</button>
        <button id="stop-blue">停止生成蓝球</button>
      </div>
    </main>

    <script>
      const redBalls = document.querySelectorAll('.red');
      const blueBall = document.querySelector('.blue');
      const startRedButton = document.getElementById('start-red');
      const stopRedButton = document.getElementById('stop-red');
      const startBlueButton = document.getElementById('start-blue');
      const stopBlueButton = document.getElementById('stop-blue');

      let isGeneratingRed = false;
      let isGeneratingBlue = false;

      function generateRandomNumber(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
      }

      function getRedBalls() {
        if (!isGeneratingRed) return;

        for (const ball of redBalls) {
          ball.textContent = generateRandomNumber(1, 40);
        }
      }

      function getBlueBall() {
        if (!isGeneratingBlue) return;

        blueBall.textContent = generateRandomNumber(1, 15);
      }

      startRedButton.addEventListener('click', () => {
        isGeneratingRed = true;
        setInterval(getRedBalls, 100);
      });

      stopRedButton.addEventListener('click', () => {
        isGeneratingRed = false;
      });

      startBlueButton.addEventListener('click', () => {
        isGeneratingBlue = true;
        setInterval(getBlueBall, 100);
      });

      stopBlueButton.addEventListener('click', () => {
        isGeneratingBlue = false;
      });
    </script>
  </body>
</html>

效果截图

图片[1]-模拟双色球开奖HTML源码-QQ沐编程

© 版权声明
THE END
喜欢就支持一下吧
点赞12赞赏 分享