当前位置: 首页 > article >正文

html css js 生成随机颜色

起因, 目的:

整理文件,发现之前写的一个小工具。

效果图

在这里插入图片描述

点击按钮会生成新的格子。
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Random Color Blocks</title>
    <style>
      .color-block {
        width: 100px;
        height: 100px;
        display: inline-block;
        margin: 5px;
      }
      #generate-button {
        justify-content: center;
        align-items: center;
        width: 350px;
        height: 80px;
        background-color: pink;
        margin-left: 300px;
        font-size: 26px; /* 增加字体大小 */
        font-weight: bold; /* 字体加粗 */
        border-radius: 5px; /* 增加圆角 */
      }

      #generate-button:hover {
        background-color: #0056b3; /* 鼠标悬停时的背景颜色 */
      }

      #color-grid {
        margin: 100px;
        border: solid 2px black;
        border-radius: 5px; /* 增加圆角 */
      }
    </style>
  </head>
  <body>
    <h1>
      <button id="generate-button">Generate New Colors</button>
    </h1>

    <div id="color-grid"></div>

    <script>
      // Function to generate a random color within the specified range
      function getRandomColor(min, max) {
        return `rgb(${Math.floor(Math.random() * (max - min + 1)) + min}, ${
          Math.floor(Math.random() * (max - min + 1)) + min
        }, ${Math.floor(Math.random() * (max - min + 1)) + min})`;
      }

      // Function to create a grid of color blocks
      function createColorGrid(size) {
        const grid = document.getElementById("color-grid");
        grid.innerHTML = ""; // Clear the grid
        for (let i = 0; i < size * size; i++) {
          const block = document.createElement("div");
          block.classList.add("color-block");
          block.style.backgroundColor = getRandomColor(10, 250);
          grid.appendChild(block);
        }
      }

      // Function to handle the button click event
      function handleButtonClick() {
        createColorGrid(9); // Start with a 3x3 grid
      }

      // Initial grid creation
      createColorGrid(9); // Start with a 3x3 grid

      // Add event listener to the button
      document
        .getElementById("generate-button")
        .addEventListener("click", handleButtonClick);
    </script>
  </body>
</html>


老哥留步,支持一下。

请求支持


http://www.kler.cn/news/358084.html

相关文章:

  • 学习笔记——交换——STP(生成树)基本概念
  • 【经验总结】Can报文能发不能收问题分析
  • MIC(麦克风)的主要参数
  • 后端参数校验方式
  • 基于微信小程序的在线点歌系统(论文+源码)-kaic
  • 进程间关系与守护进程
  • IP- guard产品版本升级指引详解
  • Scala入门基础(10.1)高阶函数2
  • Windows电脑桌面如何弄个好用的提醒备忘录?
  • PSO算法
  • unity学习-反射探针Reflection
  • EDM邮件营销,如何确保高频率发送不触发限制
  • Qt_ymode自己实现
  • redis 创建只读用户
  • HarmonyOS 鸿蒙面试第一弹
  • 边缘计算技术的优势与挑战
  • Libevent源码剖析之reactor
  • 【热门主题】000004 案例 Vue.js组件开发
  • 【从零开始的LeetCode-算法】3192. 使二进制数组全部等于 1 的最少操作次数 II
  • 1. 解读DLT698.45-2017通信规约--预连接响应