编程 一个简单的瀑布流布局实现方法,使用Vue3和Vite技术

2024-11-19 09:50:45 +0800 CST views 1317

一个简单的瀑布流布局实现方法,使用Vue3和Vite技术

背景

在工作中,我们常常需要实现瀑布流布局。今天,我将分享一个简单的实现方法,供大家参考。

技术

  • Vue 3
  • Vite

HTML

<template>
  <div>
    <button @click="addCard">增加</button>
    <div class="card">
      <div class="card-list"
           v-waterfall
           v-for="(item,index) in list" 
           :key="index"
           :style="{backgroundColor:item.color,height:item.height}">
        <p class="text"> {{ index }} </p>
      </div>
    </div>
  </div>
</template>

CSS

注意:由于在JS中使用了transform,所以需要设置定位。

.card {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  width: 700px;
}
.card-list {
  position: absolute;
  width: 200px;
}

JS

实现逻辑

  1. 声明一个高度数组,保存每列的高度。
  2. 获取最小高度的位置,方便定位。
  3. 自定义指令:v-waterfall,在mounted中获取DOM元素。
import { onMounted, ref, nextTick } from "vue";

const list = ref([
  {color:'#76da6e', height:'100px'},
  {color:'#40a988', height:'70px'},
  {color:'#2384c4', height:'120px'},
  {color:'#106ba3', height:'80px'},
  {color:'#0a589e', height:'110px'},
  {color:'#0a4e98', height:'90px'},
  {color:'#09408c', height:'100px'},
  {color:'#083680', height:'130px'},
]);
const tabulationHeights = ref([0, 0, 0, 0]);

const updateLayout = (el) => {
  const columns = getMinTabulationHeight(tabulationHeights.value);
  const top = tabulationHeights.value[columns];
  const left = columns * el.clientWidth;
  el.style.transform = `translate(${left}px,${top}px)`;
  tabulationHeights.value[columns] += el.offsetHeight;
}

const getMinTabulationHeight = (arr) => {
  let min = Math.min(...arr);
  return arr.indexOf(min) !== -1 ? arr.indexOf(min) : 0;
}

const vWaterfall = {
  mounted: (el) => {
    updateLayout(el);
  },
  updated: (el) => {
    // updateLayout(el);
  }
}

// 增加卡片
const addCard = () => {
  const height = Math.floor(Math.random() * 50 + 100);
  const color = '#' + Math.floor(Math.random() * 16777215).toString(16);
  list.value.push({color, height: height + 'px'});
}

问题与处理

有朋友可能会问:列表之间肯定不会没有间隔吧?这很简单。如果要封装一个组件,只需传递一个GAP值,参与计算即可:

const updateLayout = (el) => {
  const columns = getMinTabulationHeight(tabulationHeights.value);
  const top = tabulationHeights.value[columns] + 20; // 加上间隔
  const left = (columns * el.clientWidth) + (columns * 20); // 加上间隔
  el.style.transform = `translate(${left}px,${top}px)`;
  tabulationHeights.value[columns] += el.offsetHeight + 20; // 更新高度
}

总结

以上只是一个简单的模板,大家可以在此基础上进行优化,以满足自己的需求。今天的分享就到这里,如果觉得不错,请收藏这个源码库,后续会更新插件库!

复制全文 生成海报 前端开发 Web设计 Vue框架

推荐文章

一个收银台的HTML
2025-01-17 16:15:32 +0800 CST
Web 端 Office 文件预览工具库
2024-11-18 22:19:16 +0800 CST
小技巧vscode去除空格方法
2024-11-17 05:00:30 +0800 CST
Linux 常用进程命令介绍
2024-11-19 05:06:44 +0800 CST
Go语言中的mysql数据库操作指南
2024-11-19 03:00:22 +0800 CST
Linux查看系统配置常用命令
2024-11-17 18:20:42 +0800 CST
Flet 构建跨平台应用的 Python 框架
2025-03-21 08:40:53 +0800 CST
PHP 唯一卡号生成
2024-11-18 21:24:12 +0800 CST
阿里云发送短信php
2025-06-16 20:36:07 +0800 CST
7种Go语言生成唯一ID的实用方法
2024-11-19 05:22:50 +0800 CST
Vue3中的事件处理方式有何变化?
2024-11-17 17:10:29 +0800 CST
windows安装sphinx3.0.3(中文检索)
2024-11-17 05:23:31 +0800 CST
手机导航效果
2024-11-19 07:53:16 +0800 CST
php strpos查找字符串性能对比
2024-11-19 08:15:16 +0800 CST
Vue3中的自定义指令有哪些变化?
2024-11-18 07:48:06 +0800 CST
jQuery中向DOM添加元素的多种方法
2024-11-18 23:19:46 +0800 CST
Go配置镜像源代理
2024-11-19 09:10:35 +0800 CST
回到上次阅读位置技术实践
2025-04-19 09:47:31 +0800 CST
程序员茄子在线接单