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

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

一个简单的瀑布流布局实现方法,使用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框架

推荐文章

js一键生成随机颜色:randomColor
2024-11-18 10:13:44 +0800 CST
Nginx 状态监控与日志分析
2024-11-19 09:36:18 +0800 CST
12个非常有用的JavaScript技巧
2024-11-19 05:36:14 +0800 CST
SQL常用优化的技巧
2024-11-18 15:56:06 +0800 CST
Python上下文管理器:with语句
2024-11-19 06:25:31 +0800 CST
Golang 中应该知道的 defer 知识
2024-11-18 13:18:56 +0800 CST
JavaScript设计模式:发布订阅模式
2024-11-18 01:52:39 +0800 CST
浏览器自动播放策略
2024-11-19 08:54:41 +0800 CST
三种高效获取图标资源的平台
2024-11-18 18:18:19 +0800 CST
支付宝批量转账
2024-11-18 20:26:17 +0800 CST
mendeley2 一个Python管理文献的库
2024-11-19 02:56:20 +0800 CST
如何实现生产环境代码加密
2024-11-18 14:19:35 +0800 CST
使用Python实现邮件自动化
2024-11-18 20:18:14 +0800 CST
FastAPI 入门指南
2024-11-19 08:51:54 +0800 CST
使用 Nginx 获取客户端真实 IP
2024-11-18 14:51:58 +0800 CST
智慧加水系统
2024-11-19 06:33:36 +0800 CST
前端代码规范 - Commit 提交规范
2024-11-18 10:18:08 +0800 CST
如何在Vue3中定义一个组件?
2024-11-17 04:15:09 +0800 CST
在Vue3中实现代码分割和懒加载
2024-11-17 06:18:00 +0800 CST
Go 如何做好缓存
2024-11-18 13:33:37 +0800 CST
使用Ollama部署本地大模型
2024-11-19 10:00:55 +0800 CST
程序员茄子在线接单