【问题标题】:Background color changing tiles背景颜色变化的瓷砖
【发布时间】:2023-03-11 18:52:01
【问题描述】:

这更像是一个包含一些代码片段的理论问题。我想创建一个背景充满颜色和形状不断变化的瓷砖的网站。一个很好的例子是 Windows Phone 8 磁贴。当然,稍后我想创建一个更复杂的效果,但我想知道您将如何单独更改每个 50x50 像素的正方形。

这是一个不断改变背景颜色的代码片段。

.animate {
    height: 200px;
    width: 400px;
    border: 1px solid #000;
    -webkit-animation: animate_bg 5s;
    animation: animate_bg 5s;
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
}

@keyframes animate_bg {
    0%   {background:red;}
    50%  {background:green;}
    100% {background:blue;}
}

@keyframes animate_bg {
    0%   {background:red;}
    50%  {background:green;}
    100% {background:blue;}
}

@-webkit-keyframes animate_bg {
    0%   {background:red;}
    50%  {background:green;}
    100% {background:blue;}
}

现在我需要一次又一次地创建这种效果,直到它填满我的页面,还是有更好的方法?也许有一个类似于 bg-repeat 的“随机重复”功能?注意:瓷砖的起始颜色应该是随机的。

谢谢!

【问题讨论】:

    标签: javascript php ajax html css


    【解决方案1】:

    当然有更好的方法(当您发现自己重复代码时通常会出现这种情况)。

    对动画效果使用 CSS 过渡,定义许多类 - 每个类都有不同的颜色,并使用 JavaScript 随机分配它们。

    这是一个简单的例子,展示了我在上面写的内容。它支持无限数量的瓷砖和颜色。

    var tiles = document.getElementsByClassName('tile');
    var totalTypes = 5;
    var interval = 1000;
    
    function paintTiles() {
      for (var i = 0; i < tiles.length; i++) {
        tiles[i].className = 'tile'; // reset classes
        tiles[i].classList.add('type' + Math.ceil(Math.random() * totalTypes));
      }
    }
    paintTiles(); // initial paint
    setInterval(paintTiles, interval); // paint afterwards
    .tile {
      display: inline-block;
      width: 50px;
      height: 50px;
      border: 1px solid black;
      transition: 1s all linear;
    }
    
    .type1 {
      background: red;
    }
    
    .type2 {
      background: blue;
    }
    
    .type3 {
      background: green;
    }
    
    .type4 {
      background: yellow;
    }
    
    .type5 {
      background: chucknorris; 
      /*  
      :)
      inspired by: http://stackoverflow.com/questions/8318911/why-does-html-think-chucknorris-is-a-color
      */
    }
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <br>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>
    <div class="tile"></div>

    更新

    好的,这里是使用 JS 动态添加图块,尽管从服务器端执行此操作可能是一个更好的主意。当然,你可以控制你想要多少个tile,我以10个为例。

    var totalTiles = 10;
    var tiles = [];
    var totalTypes = 5;
    var interval = 1000;
    
    
    function addTiles() {
      for (var i = 0; i < totalTiles; i++) {
        var div = document.createElement('div');
        div.className = 'tile';
        div.classList.add('type' + Math.ceil(Math.random() * totalTypes));
        document.body.appendChild(div);
      }
      tiles = document.getElementsByClassName('tile');
    }
    
    function paintTiles() {
      for (var i = 0; i < tiles.length; i++) {
        tiles[i].className = 'tile'; // reset classes
        tiles[i].classList.add('type' + Math.ceil(Math.random() * totalTypes));
      }
    }
    paintTiles(); // initial paint
    setInterval(paintTiles, interval); // paint afterwards
    .tile {
      display: inline-block;
      width: 50px;
      height: 50px;
      border: 1px solid black;
      transition: 1s all linear;
    }
    
    .type1 {
      background: red;
    }
    
    .type2 {
      background: blue;
    }
    
    .type3 {
      background: green;
    }
    
    .type4 {
      background: yellow;
    }
    
    .type5 {
      background: chucknorris; 
      /*  
      :)
      inspired by: http://stackoverflow.com/questions/8318911/why-does-html-think-chucknorris-is-a-color
      */
    }
    &lt;button onclick="addTiles()"&gt;Add 10 Tiles&lt;/button&gt;&lt;br /&gt;

    【讨论】:

    • 所以如果我要用瓷砖填充整个背景,我是否必须复制/粘贴
      200 次?没有更好的办法吗?
    • 当然还有更好的办法。您可以使用 JS 或服务器端语言来生成它们。要我教你怎么做吗?
    • 如果不太麻烦的话,我很乐意。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签