当前位置: 首页 > news >正文

瀑布流布局 vue

一种是使用纯 JavaScript 来实现,另一种是使用第三方库 Masonry.js

方案一:使用纯 JavaScript 实现

这种方法适用于想要完全控制瀑布流布局的情况。

步骤 1:HTML 结构

首先,在 HTML 中定义一个容器来存放瀑布流中的元素。

<div id="waterfall"><!-- 瀑布流中的元素将动态插入到这里 -->
</div>
步骤 2:Vue 组件

接下来,创建一个 Vue 组件来处理瀑布流逻辑。

<template><div id="waterfall" ref="waterfall"><div v-for="(item, index) in items" :key="index" class="item" :style="{height: item.height + 'px'}"><img :src="item.src" :alt="item.alt" @load="onImgLoad(index)"></div></div>
</template><script>
export default {data() {return {items: [// 示例数据{ src: 'image1.jpg', height: 200, alt: 'image1' },{ src: 'image2.jpg', height: 300, alt: 'image2' },// 更多数据...],columns: []};},methods: {onImgLoad(index) {const item = this.items[index];const shortestColumn = this.columns.reduce((minCol, currentCol) => currentCol.height < minCol.height ? currentCol : minCol, this.columns[0]);shortestColumn.height += item.height;shortestColumn.items.push(item);this.reorderColumns();},reorderColumns() {this.columns.sort((a, b) => a.height - b.height);},init() {const waterfall = this.$refs.waterfall;this.columns = [];this.items.forEach((item, index) => {const column = this.columns.length > 0 ? this.columns[0] : { height: 0, items: [] };const div = document.createElement('div');div.className = 'column';const img = document.createElement('img');img.src = item.src;img.alt = item.alt;img.onload = () => {item.height = img.offsetHeight;column.height += img.offsetHeight;column.items.push(item);this.reorderColumns();this.$forceUpdate(); // 强制更新视图};div.appendChild(img);waterfall.appendChild(div);});}},mounted() {this.init();}
}
</script><style scoped>
#waterfall {column-count: 3; /* 初始列数 */column-gap: 10px;margin: 0 auto;max-width: 900px;
}.item {break-inside: avoid;page-break-inside: avoid;overflow: hidden;
}
</style>

方案二:使用 Masonry.js

如果你希望使用一个成熟的库来简化瀑布流的实现,可以选择 Masonry.js。

步骤 1:安装 Masonry.js

首先,你需要安装 Masonry.js:

npm install masonry-layout
步骤 2:引入 Masonry.js

然后,在你的 Vue 组件中引入 Masonry.js。

<template><div id="masonry" class="masonry"><div v-for="item in items" :key="item.id" class="item"><img :src="item.src" :alt="item.alt"></div></div>
</template><script>
import Masonry from 'masonry-layout';export default {data() {return {items: [// 示例数据{ id: 1, src: 'image1.jpg', alt: 'image1' },{ id: 2, src: 'image2.jpg', alt: 'image2' },// 更多数据...]};},mounted() {this.initMasonry();},methods: {initMasonry() {this.msnry = new Masonry('#masonry', {itemSelector: '.item',columnWidth: '.item'});this.items.forEach(item => {this.$nextTick(() => {this.msnry.appended(document.querySelector(`[data-id="${item.id}"]`));this.msnry.layout();});});}}
}
</script><style scoped>
.masonry {column-count: 3; /* 初始列数 */column-gap: 10px;margin: 0 auto;max-width: 900px;
}.item {break-inside: avoid;page-break-inside: avoid;overflow: hidden;
}
</style>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 数据源10min自动断开连接导致查询抛异常(未获取可用连接)
  • Fast-BEV: A Fast and Strong Bird’s-Eye ViewPerception Baseline
  • 安防视频汇聚平台EasyCVR启动后无法访问登录页面是什么原因?
  • Unity | 基础知识
  • 深入理解并实现——归并排序【C语言】
  • 学生用什么类型投影仪比较好?2024开学季最值得买的装备当贝X5S投影仪
  • 10:USB模块布局
  • STM32H750VBT6烧录源码无反应的问题
  • 应用层(Web与HTTP)
  • docker基本使用及常见问题
  • 零基础入门天池镜像提交--windows场景VirtualBox虚拟机安装linux系统并ssh远程登录,直至镜像的制作及提交
  • 前波士顿咨询Platinion董事总经理陈果加入望繁信科技
  • 和星辰为伴,与代码共舞
  • 直播商城小程序开发指南:基于多商户商城系统源码的实现
  • .net开发日常笔记(持续更新)
  • [数据结构]链表的实现在PHP中
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • 【跃迁之路】【477天】刻意练习系列236(2018.05.28)
  • C++11: atomic 头文件
  • FastReport在线报表设计器工作原理
  • FineReport中如何实现自动滚屏效果
  • golang中接口赋值与方法集
  • Java编程基础24——递归练习
  • MySQL主从复制读写分离及奇怪的问题
  • 半理解系列--Promise的进化史
  • 搭建gitbook 和 访问权限认证
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 番外篇1:在Windows环境下安装JDK
  • 分享自己折腾多时的一套 vue 组件 --we-vue
  • 盘点那些不知名却常用的 Git 操作
  • 前端自动化解决方案
  • 深入浅出webpack学习(1)--核心概念
  • 一个项目push到多个远程Git仓库
  • 与 ConTeXt MkIV 官方文档的接驳
  • 浅谈sql中的in与not in,exists与not exists的区别
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • ​【经验分享】微机原理、指令判断、判断指令是否正确判断指令是否正确​
  • #13 yum、编译安装与sed命令的使用
  • #AngularJS#$sce.trustAsResourceUrl
  • #Z2294. 打印树的直径
  • $ git push -u origin master 推送到远程库出错
  • (1)svelte 教程:hello world
  • (16)Reactor的测试——响应式Spring的道法术器
  • (2024)docker-compose实战 (9)部署多项目环境(LAMP+react+vue+redis+mysql+nginx)
  • (MATLAB)第五章-矩阵运算
  • (二) 初入MySQL 【数据库管理】
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题
  • (一)Java算法:二分查找
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (转载)CentOS查看系统信息|CentOS查看命令
  • (最新)华为 2024 届秋招-硬件技术工程师-单板硬件开发—机试题—(共12套)(每套四十题)
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。