位置: IT常识 - 正文

vue+uniapp瀑布流布局多种实现方式(vue实现瀑布流布局)

编辑:rootadmin
vue+uniapp瀑布流布局多种实现方式 文章目录前言一、实现原理二、代码实现1.方式1(图片高度累加比较法)2.方式2(父元素高度比较法)三.uniapp实现代码实现四、多列实现代码实现前言

推荐整理分享vue+uniapp瀑布流布局多种实现方式(vue实现瀑布流布局),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue瀑布流式页面布局,uicollectionview 瀑布流,uniapp实现瀑布流,vue瀑布流实现,vue实现瀑布流及无限加载,vue瀑布流插件最好用的,uniapp实现瀑布流,vue瀑布流实现,内容如对您有帮助,希望把文章链接给更多的朋友!

瀑布流布局是网页设计常见的一种布局,一般用于图片多列展示。列宽固定,图片根据自身高度自适应交错排列。

一、实现原理

通过动态计算哪一列高度最低,就把图片放置该列下显示,直至所有图片分列完毕

计算哪一列高度最低具体实现过程又分2种方式:

vue+uniapp瀑布流布局多种实现方式(vue实现瀑布流布局)

方式1:通过计算每一列每张图片渲染后高度进行累加就是该列的高度,记录每列累加高度比较大小 方式2:直接通过图片父级元素高度(列div)来判断哪一列最低

区别:方式1无需等待图片真实渲染完成在比较高度,方式2需要等待图片真实渲染完成在获取高度

二、代码实现

以左右2列为例

<template> <div class="page"> <!-- 左图片列表 --> <div class="left" ref="left"> <img class="img" v-for="(item, index) in leftList" :key="index" :src="item" /> </div> <!-- 右图片列表 --> <div class="right" ref="right"> <img class="img" v-for="(item, index) in rightList" :key="index" :src="item" /> </div> </div></template><style scoped>.page { width: 100%; display: flex; align-items: flex-start; padding: 0 1%; box-sizing: border-box;}.left,.right { margin: 0 auto; width: 48%;}.img { width: 100%; height: auto; margin-bottom: 10px;}</style>1.方式1(图片高度累加比较法)<script>export default { data() { return { imgList: [ "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082", "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500", "https://img1.baidu.com/it/u=3866290852,3566512524&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500", "https://img2.baidu.com/it/u=1114729443,1120710416&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500", "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082", "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "https://img2.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500", ], //所有图片 leftList: [], //左边列图片 rightList: [], //右边列图片 leftHeight: 0, //左边列高度 rightHeight: 0, //右边列高度 columnWidth: 0, //列宽度 }; }, mounted() { this.$nextTick(() => { this.columnWidth = this.$refs.left.clientWidth; this.setWaterFallLayout(); }); }, methods: { //方法1 async setWaterFallLayout() { for (let item of this.imgList) { let img = new Image(); img.src = item; try{ let h = await this.getImgHeight(img);//图片渲染后高度 if (this.leftHeight <= this.rightHeight) {//左边列比右边低,图片放入左边 this.leftList.push(item); this.leftHeight += h; } else {//否则,图片放入右边 this.rightList.push(item); this.rightHeight += h; } }catch(e){ console.log(e) } } }, //获取图片高度 getImgHeight(img) { return new Promise((resolve,reject) => { //图片加载完成 img.onload = () => { let h = (img.height / img.width) * this.columnWidth;//计算图片渲染后高度 resolve(h); }; //加载出错 img.onerror=()=>{ reject('error') } }); }, },};</script>2.方式2(父元素高度比较法)

每次放入图片需要等待渲染后再重新计算父元素高度,关键代码 await this.$nextTick()

<script>export default { data() { return { imgList: [ "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082", "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500", "https://img1.baidu.com/it/u=3866290852,3566512524&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500", "https://img2.baidu.com/it/u=1114729443,1120710416&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500", "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082", "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "https://img2.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500", ], //所有图片 leftList: [], //左边列表图片 rightList: [], //右边列表图片 }; }, mounted() { this.$nextTick(() => { this.setWaterFallLayout2(); }); }, methods: { //方法2 async setWaterFallLayout2() { for (let item of this.imgList) { if (this.$refs.left.clientHeight <= this.$refs.right.clientHeight) {//左边列比右边低,图片放入左边 this.leftList.push(item); } else {//否则图片放入右边 this.rightList.push(item); } await this.$nextTick();//等待渲染完成后重新比较左右高度 } }, },};</script>三.uniapp实现

由于uniapp获取元素高度和vue有所区别,造成实现瀑布流方式也需要调整。我们知道uniapp不能通过this.$ref.xx.clientHeight获取元素高度,而需要通过uni.createSelectorQuery().in(this).select(‘.xxxx’).boundingClientRect().exec()来获取,且经过实测当图片动态加入列后通过该api计算出父元素真实高度是不准确的,所以uniapp瀑布流布局实现方式只能通过方法1(也即图片高度累加法)进行实现,除了上面方法1通过img.onload来获取图片高度外,uniapp还提供uni.getImageInfo方法来更方便获取图片高度。

代码实现<template><view class="page"><view class="left" ref="left"><image class="image" v-for="(item,i) in leftList" :key="i" :src="item" mode="widthFix"></image></view><view class="right" ref="right"><image class="image" v-for="(item,i) in rightList" :key="i" :src="item" mode="widthFix"></image></view></view></template><style lang="scss">.page {width: 100%;display: flex;align-items: flex-start;padding: 0 1%;box-sizing: border-box;}.left,.right {margin: 0 auto;width: 48%;}.image {width: 100%;height: auto;margin-bottom: 10px;}</style><script>export default {data() {return {imageList: ["https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082","https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500","https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500","https://img1.baidu.com/it/u=3866290852,3566512524&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500","https://img2.baidu.com/it/u=1114729443,1120710416&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500","https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082","https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500","https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",], //所有图片leftList: [], //左边列图片rightList: [], //右边列图片leftHeight: 0, //左边列高度rightHeight: 0, //右边列高度columnWidth: 0 //列宽度}},mounted() {this.$nextTick(() => {uni.createSelectorQuery().in(this).select('.left').boundingClientRect(res => {this.columnWidth = res.width//方法1this.setWaterFallLayout()//方法2// this.setWaterFallLayout2()}).exec()})},methods: {//方法1通过img.onloadasync setWaterFallLayout() {for (let item of this.imageList) {let img = new Image()img.src = itemtry {let h = await this.getImgHeight(img)if (this.leftHeight <= this.rightHeight) {this.leftList.push(item)this.leftHeight += h} else {this.rightList.push(item)this.rightHeight += h}} catch (e) {console.log(e)}}},//获取图片高度getImgHeight(img) {return new Promise((resolve, reject) => {img.onload = () => {let h = img.height / img.width * this.columnWidthresolve(h)}//加载出错img.onerror = () => {reject('error')}})},//方法2通过uni.getImageInfoasync setWaterFallLayout2() {for (let item of this.imageList) {uni.getImageInfo({src: item,success: e => {if (this.leftHeight <= this.rightHeight) {this.leftList.push(item)this.leftHeight += e.height} else {this.rightList.push(item)this.rightHeight += e.height}}})}}},}</script>

四、多列实现

多列实现和2列一样,动态生成每列图片数据和记录每列高度

代码实现

以最简单的父元素高度比较法(方式2)为例实现,图片高度累加比较法(方式1)自行类比实现

<template> <div class="page"> <div class="column" ref="column" v-for="(item, index) in columnList" :key="index" > <img class="img" v-for="(n, i) in item" :key="i" :src="n" /> </div> </div></template><style scoped>.page { width: 100%; display: flex; align-items: flex-start; padding: 0 1%; box-sizing: border-box;}.column { flex: 1; padding: 0 10px; box-sizing: border-box; width: 0;}.img { width: 100%; height: auto; margin-bottom: 10px;}</style><script>export default { data() { return { imgList: [ "https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082","https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500","https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500","https://img1.baidu.com/it/u=3866290852,3566512524&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500","https://img2.baidu.com/it/u=1114729443,1120710416&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500","https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082","https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500","https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500","https://img0.baidu.com/it/u=1345303087,1528317222&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=1082","https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", ], //所有图片 columnList: [], //分配后的每列图片 columWidth: 0, //每列宽度 columnCount: 5, //显示几列 }; }, created() { //初始化数据 for (let i = 0; i < this.columnCount; i++) { this.columnList.push([]);//生成每列图片数组 } }, mounted() { this.$nextTick(()=>{ this.setWaterFallLayout(); }) }, methods: { //瀑布布局 async setWaterFallLayout() { for (let item of this.imgList) { let columnHeight = this.$refs.column.map((item) => item.clientHeight); //每列高度数组 let min = Math.min(...columnHeight); //找出最小高度值 let index = columnHeight.findIndex((item) => item === min); //找出最小高度列的索引 this.columnList[index].push(item);//放入图片 await this.$nextTick(); //等待渲染完成后重新比较高度 } }, },};</script>
本文链接地址:https://www.jiuchutong.com/zhishi/294547.html 转载请保留说明!

上一篇:Video.js 使用教程 - 手把手教你基于 Vue 搭建 HTML 5 视频播放器(video.js能播放什么格式)

下一篇:什么是WebRTC?(什么是webrip)

  • 税务局退给企业的多交的所得税该怎样帐务处理?
  • 企业纳税人是什么
  • 汇票签收后可以撤回吗
  • 超过库存现金限额的现金要及时存入银行
  • 银行会计核算方法的特点
  • 办理个体户营业执照需要什么条件
  • 跨境电商的钱怎么到账
  • 民非企业附加税入什么科目
  • 增值税逾期未申报网上可以吗
  • 租赁期间怎么算
  • 购买的车位是否有产权证
  • 已认证的红字发票怎么退
  • 货发出款未收的会计分录怎么做?
  • 我国流转税主要包括
  • 工程技术服务费和技术服务费
  • 工会经费应该计入人工成本吗
  • 当期销项税额等于什么乘以什么
  • 自主就业退役士兵从事个体经营的
  • 个人社保部分工资计入哪个科目
  • 盈余公积什么时候调整
  • 一般纳税人税控盘维护费会计分录
  • 初级备考心得总结
  • 公司转让税费如何计算
  • 非营利组织免税条件发生变化
  • 股权代持分红免税吗
  • 委托代理出口能否办理退税
  • 购买公司付款会计分录
  • 发票已开货没发出的账务处理?
  • 电脑没有声音问题
  • php的认识
  • nclaunch.exe - nclaunch进程有什么用 是什么意思
  • 股权转让要交什么税举例
  • 橡皮树怎么养护
  • 高新技术企业如何在电子税务局备案
  • 制造费用属于期间费用期末一定无余额吗
  • mksysb命令
  • phpinfo() 中 Local Value(局部变量)Master Value(主变量) 的区别
  • yolov5的使用
  • 训练自己的GPT
  • 研发人员工资计入什么科目
  • 会展中心高新技术展览
  • 参展费会计分录
  • 收回多发的工资在上缴财政,可以用应缴财政款科目吗
  • 坏账准备检查情况表怎么填
  • 简述SQL server管理器的功能
  • sql server中数据文件的扩展名是
  • 资产管理业务是表外业务吗
  • 帮员工购买意外险怎么记到分录
  • 如何评价福建省
  • 消费税如何计量
  • 工业企业新产品收入的调研报告
  • 用于出口的进项发票怎么做账
  • 公司购买的车辆折旧年限
  • 固定资产内部抵扣增值税
  • 投资性房地产涉及其他综合收益
  • 资产负债表日后非调整事项应当在附注中披露
  • 影响以前年度损益的科目
  • 企业出售投资性房地产应按照售价与账面价值
  • 领购发票的方式有哪些
  • 公司发放员工工资不走账
  • 财务月末暂估成本是什么
  • 一分钟教你
  • winxp系统如何安装
  • pps影音怎么没有了
  • windows7命名规则
  • youphone.exe是什么
  • 文件夹底部显示
  • windows右键不能用了
  • node.js的流的作用
  • Android---60---Notification 通知栏的简单使用
  • unity做安卓app
  • Unity3D游戏开发(第2版)pdf
  • hbase shell split
  • python怎么在网上赚钱
  • 黄南同仁县海拔多少米
  • 国家税务总局洛阳市分局
  • 开票员怎么登录电子税务局进行开票验证
  • 组织创新包括哪些类型
  • 12333热线时间
  • 硅矿的合法开采方法
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设