head> meta charset="UTF-8" /> meta name="viewport" content="width=device-w..."/>

html css js 生成随机颜色

8,277次阅读
没有评论

共计 1312 个字符,预计需要花费 4 分钟才能阅读完成。

起因,目的:

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

效果图

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 Blockstitle>
    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 Colorsbutton>
    h1>

    div id="color-grid">div>

    script>
      
      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 createColorGrid(size) {
        const grid = document.getElementById("color-grid");
        grid.innerHTML = ""; 
        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 handleButtonClick() {
        createColorGrid(9); 
      }

      
      createColorGrid(9); 

      
      document
        .getElementById("generate-button")
        .addEventListener("click", handleButtonClick);
    script>
  body>
html>


老哥留步,支持一下。

请求支持

原文地址: html css js 生成随机颜色

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-10-30发表,共计1312字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)