我确实有5个宽度/高度完全相同的图块,我想知道是否可以将它们随机设置为背景。
position: relative;
background-image: url(assets/images/bg_tile1.png), url(assets/images/bg_tile2.png), url(assets/images/bg_tile3.png), url(assets/images/bg_tile4.png), url(assets/images/bg_tile5.png);
background-size: auto;
background-repeat: repeat;
到目前为止,我已经知道了,该如何随机重复?就像它们在网格中一样(随机):
1 4 2 1
2 3 4 1
3 1 5 5
答案 0 :(得分:3)
您可以使用SVG对此进行近似。您将构建一个随机模式,该模式将重复进行。它不会是完全随机的,但这是一个很好的近似值。
下面是一个3x3模式的示例:
svg {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<pattern id="bgimg" x="0" y="0" width="300" height="300" patternUnits="userSpaceOnUse">
<image xlink:href="https://picsum.photos/id/1055/100/100.jpg" x="0" y="0" height="100" width="100" />
<image xlink:href="https://picsum.photos/id/1074/100/100.jpg" x="0" y="100" height="100" width="100" />
<image xlink:href="https://picsum.photos/id/1080/100/100.jpg" x="0" y="200" height="100" width="100" />
<image xlink:href="https://picsum.photos/id/1074/100/100.jpg" x="100" y="0" height="100" width="100" />
<image xlink:href="https://picsum.photos/id/1065/100/100.jpg" x="100" y="100" height="100" width="100" />
<image xlink:href="https://picsum.photos/id/1039/100/100.jpg" x="100" y="200" height="100" width="100" />
<image xlink:href="https://picsum.photos/id/117/100/100.jpg" x="200" y="0" height="100" width="100" />
<image xlink:href="https://picsum.photos/id/1024/100/100.jpg" x="200" y="100" height="100" width="100" />
<image xlink:href="https://picsum.photos/id/1025/100/100.jpg" x="200" y="200" height="100" width="100" />
</pattern>
</defs>
<rect x="0" y="0" width="10000" height="10000" fill="url(#bgimg)" />
</svg>