简单瀑布流

简介: 简单瀑布流

瀑布流

什么是瀑布流

又称为瀑布流布局,是一种比较经典的网站布局方式,尤其多见于图片较多的页面。常见有两种瀑布流方式。分别为

  1. 定宽不定高
  2. 定高不定宽

定宽不定高

定高不定宽

IU~X(C1XH(%9E4N{E3OUQ_F.png

定宽不定高-思路

  1. 动态生成 1000 个 li 标签,设置统一的宽度 设置透明度 (实际开发中,只需要获取 1000 个图片对应的高度即可)
  2. 开启定时器,每一行只放 3 个 li 标签 ,三个 li 标签,按照正常顺序依次摆放即可,后面的 li 标签切换逻辑
  3. 每显示一个 li 标签,就需要自己维护好一个数组 [第 1 列的 li 标签高度集合,第 2 列的 li 标签高度集合,第 3 列的 li 标签高度集合]
  4. 统计出那一列 最低 和 对应的下标(0,1,2)
  5. 将下一个 li 标签 放在对应下标的位置,同时计算出下标对应列的高度总和,设置为 即将显示的 li 标签的 top

代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      li {
        width: 33.33%;
        border: 1px solid #000;
        font-size: 30px;
        text-align: center;
        position: absolute;
        top: 0;
        left: 0;
        list-style: none;
      }
    </style>
  </head>
  <body>
    <ul></ul>
    <script>
      init();
      function init() {
        const ul = document.querySelector("ul");
        for (let index = 0; index < 1000; index++) {
          const li = document.createElement("li");
          li.innerText = index;
          li.style.backgroundColor = getRandomColor();
          li.style.height =
            Math.floor(Math.random() * (200 - 50 + 1) + 50) + "px";
          li.style.opacity = "0";
          ul.appendChild(li);
        }
        function getRandomColor() {
          const color1 = Math.floor(Math.random() * 256);
          const color2 = Math.floor(Math.random() * 256);
          const color3 = Math.floor(Math.random() * 256);
          return `rgb(${color1},${color2},${color3})`;
        }
        setWaterFall();
        function setWaterFall() {
          const lis = [...document.querySelectorAll("li")];
          let index = 0;
          const heightList = [[], [], []];
          const timeId = setInterval(() => {
            const li = lis.shift();
            if (!li) {
              clearInterval(timeId);
              return;
            }
            if (index < 3) {
              const left = index * li.clientWidth + "px";
              li.style.left = left;
              heightList[index].push(li.clientHeight);
            } else {
              // 计算出3个列分别对应的高度 [1,2,3]
              const sumHeightList = heightList.reduce(
                (sumColumnHeight, columnList) => [
                  ...sumColumnHeight,
                  columnList.reduce((a, b) => a + b, 0),
                ],
                []
              );
              const { minIndex, minHeight } = getMinIndex(sumHeightList);
              const left = minIndex * li.clientWidth + "px";
              const top = minHeight + "px";
              li.style.left = left;
              li.style.top = top;
              heightList[minIndex].push(li.clientHeight);
            }
            li.style.opacity = "1";
            index++;
          }, 500);
        }
        function getMinIndex(arr) {
          let minIndex = 0;
          let minHeight = arr[0];
          for (let index = 0; index < arr.length; index++) {
            const element = arr[index];
            if (minHeight > element) {
              minHeight = element;
              minIndex = index;
            }
          }
          return { minIndex, minHeight };
        }
      }
    </script>
  </body>
</html>

效果


目录
相关文章
awk 常用的命令样例
以下是一些常用的 awk 命令样例: 1. 打印文件的某列: ``` awk '{print $1}' filename.txt ``` 这个命令会打印文件 `filename.txt` 的第一列。 2. 根据条件筛选行并打印: ``` awk '$3 > 10 {print $1, $2}' filename.txt ``` 这个命令会打印文件 `filename.txt` 中第三列大于 10 的行的第一列和第二列。 3. 计算并打印某列的总和: ``` awk '{sum += $1} END {print sum}' filename.txt ``` 这个命令会计算文件 `file
431 0
|
测试技术 Perl
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8072 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18727 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10118 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1522 5
|
9天前
|
云安全 数据采集 人工智能