手把手带你使用Vue实现一个图片水平瀑布流插件

 2781

如何使用Vue实现一个图片水平瀑布流插件?这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助。


手把手带你使用Vue实现一个图片水平瀑布流插件


一.需求来源

今天碰到了一个需求,需要在页面里,用水平瀑布流的方式,将一些图片进行加载,这让我突然想起我很久以前写的一篇文章《JS两种方式实现水平瀑布流布局

但是有个问题,这个需求是Vue项目的,那没办法,这里给大家分享下我的开发过程,项目主体用的是之前在学习的CRMEB的后端框架来开发,UI使用iView-UI,其余的场景与其他的vue项目一致。

二.逻辑设想

如果不是vue环境,我们的逻辑为

1.获取所有的p元素

2.获取盒子的宽度,宽度都是相同,高度不同

3.在浮动布局中每一行的盒子个数不固定,是根据屏幕宽度和盒子宽度决定

4.获取屏幕宽度

5.求出列数,屏幕宽度 / 盒子宽度 取整

6.瀑布流最关键的是第二行的盒子的排布方式,通过获取第一行盒子中最矮的一个的下标,绝对定位,top是最矮盒子的高度,left是最矮盒子的下标 * 盒子的宽度

7.循环遍历所有的盒子,通过列数找到第一行所有的盒子,将第一行盒子的高度放入数组,再取出数组中最小的一个的下标,就是第6步思路的第一行盒子中最矮盒子的下标。

8.循环继续,第二行第一个盒子,通过绝对定位,放进页面。

9.关键,需要将数组中最小的值加上放进的盒子的高度

10.继续循环,遍历所有

11.如果想要加载更多,需要判断最后一个盒子的高度和页面滚动的距离,再将数据通过创建元素,追加进页面,再通过瀑布流布局展示

但如果是Vue项目,我们可以把逻辑归结为以下几步

1.获取屏幕宽度

2..获取盒子的宽度,宽度都是相同,高度不同

3.在浮动布局中每一行的盒子个数不固定,是根据屏幕宽度和盒子宽度决定

4.求出列数,屏幕宽度 / 盒子宽度 取整

5.瀑布流最关键的是第二行的盒子的排布方式,通过获取第一行盒子中最矮的一个的下标,绝对定位,top是最矮盒子的高度,left是最矮盒子的下标 * 盒子的宽度

6.继续循环,遍历所有

7.如果想要加载更多,需要判断最后一个盒子的高度和页面滚动的距离,再将数据通过创建元素,追加进页面,再通过瀑布流布局展示


三.最终效果图片

手把手带你使用Vue实现一个图片水平瀑布流插件


四.代码分析

先看下我的html部分

  1. <template>
  2.   <div class="tab-container" id="tabContainer">
  3.     <div class="tab-item" v-for="(item, index) in pbList" :key="index">
  4.       <img :src="item.url" />
  5.     </div>
  6.   </div>
  7. </template>
  8.   
  9. <style scoped>
  10. * {
  11.   margin: 0;
  12.   padding: 0;
  13. }
  14. /* 最外层大盒子 */
  15. .tab-container {
  16.   padding-top: 20px;
  17.   position: relative;
  18. }
  19. /* 每个小盒子 */
  20. .tab-container .tab-item {
  21.   position: absolute;
  22.   height: auto;
  23.   border: 1px solid #ccc;
  24.   box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  25.   background: white;
  26.   /* 元素不能中断显示 */
  27.   break-inside: avoid;
  28.   text-align: center;
  29. }
  30. .tab-container .tab-item img {
  31.   width: 100%;
  32.   height: auto;
  33.   display: block;
  34. }
  35. </style>

核心js部分

  1. export default {
  2.   name:'compList',
  3.   props:{
  4.     pbList:{
  5.       type:Array,
  6.       default:()=>{return []}
  7.     }
  8.   },
  9.   data() {
  10.     return {
  11.     };
  12.   },
  13.   mounted() {
  14.     this.$nextTick(()=>{
  15.       this.waterFall("#tabContainer", ".tab-item"); //实现瀑布流
  16.     })
  17.   },
  18.   methods: {
  19.     waterFall(
  20.         wrapIdName,
  21.         contentIdName,
  22.         columns = 5,
  23.         columnGap = 20,
  24.         rowGap = 20
  25.     ) {
  26.       // 获得内容可用宽度(去除滚动条宽度)
  27.       const wrapContentWidth =
  28.           document.querySelector(wrapIdName).offsetWidth;
  29.   
  30.       // 间隔空白区域
  31.       const whiteArea = (columns - 1) * columnGap;
  32.   
  33.       // 得到每列宽度(也即每项内容宽度)
  34.       const contentWidth = parseInt((wrapContentWidth - whiteArea) / columns);
  35.   
  36.       // 得到内容项集合
  37.       const contentList = document.querySelectorAll(contentIdName);
  38.   
  39.       // 成行内容项高度集合
  40.       const lineConentHeightList = [];
  41.   
  42.       for (let i = 0; i < contentList.length; i++) {
  43.         // 动态设置内容项宽度
  44.         contentList[i].style.width = contentWidth + "px";
  45.   
  46.         // 获取内容项高度
  47.         const height = contentList[i].clientHeight;
  48.   
  49.         if (< columns) {
  50.           // 第一行按序布局
  51.           contentList[i].style.top = "0px";
  52.           contentList[i].style.left = contentWidth * i + columnGap * i + "px";
  53.   
  54.           // 将行高push到数组
  55.           lineConentHeightList.push(height);
  56.         } else {
  57.           // 其他行
  58.           // 获取数组最小的高度 和 对应索引
  59.           let minHeight = Math.min(...lineConentHeightList);
  60.           let index = lineConentHeightList.findIndex(
  61.               (listH) => listH === minHeight
  62.           );
  63.   
  64.           contentList[i].style.top = minHeight + rowGap +"px";
  65.           contentList[i].style.left = (contentWidth + columnGap) * index + "px";
  66.   
  67.           // 修改最小列的高度 最小列的高度 = 当前自己的高度 + 拼接过来的高度 + 行间距
  68.           lineConentHeightList[index] += height + rowGap;
  69.         }
  70.       }
  71.     },
  72.   },
  73. };

这里要给大家提个醒,在当插件使用的时候,我们需要用this.$nextTick()来进行页面初始化,因为方法成功的前提是要等页面初始化加载完毕后才能进行获取和计算


总体插件代码为:

  1. <template>
  2.   <div class="tab-container" id="tabContainer">
  3.     <div class="tab-item" v-for="(item, index) in pbList" :key="index">
  4.       <img :src="item.url" />
  5.     </div>
  6.   </div>
  7. </template>
  8.   
  9. <script>
  10. export default {
  11.   name:'compList',
  12.   props:{
  13.     pbList:{
  14.       type:Array,
  15.       default:()=>{return []}
  16.     }
  17.   },
  18.   data() {
  19.     return {
  20.     };
  21.   },
  22.   mounted() {
  23.     this.$nextTick(()=>{
  24.       this.waterFall("#tabContainer", ".tab-item"); //实现瀑布流
  25.     })
  26.   },
  27.   methods: {
  28.     waterFall(
  29.         wrapIdName,
  30.         contentIdName,
  31.         columns = 5,
  32.         columnGap = 20,
  33.         rowGap = 20
  34.     ) {
  35.       // 获得内容可用宽度(去除滚动条宽度)
  36.       const wrapContentWidth =
  37.           document.querySelector(wrapIdName).offsetWidth;
  38.   
  39.       // 间隔空白区域
  40.       const whiteArea = (columns - 1) * columnGap;
  41.   
  42.       // 得到每列宽度(也即每项内容宽度)
  43.       const contentWidth = parseInt((wrapContentWidth - whiteArea) / columns);
  44.   
  45.       // 得到内容项集合
  46.       const contentList = document.querySelectorAll(contentIdName);
  47.   
  48.       // 成行内容项高度集合
  49.       const lineConentHeightList = [];
  50.   
  51.       for (let i = 0; i < contentList.length; i++) {
  52.         // 动态设置内容项宽度
  53.         contentList[i].style.width = contentWidth + "px";
  54.   
  55.         // 获取内容项高度
  56.         const height = contentList[i].clientHeight;
  57.   
  58.         if (< columns) {
  59.           // 第一行按序布局
  60.           contentList[i].style.top = "0px";
  61.           contentList[i].style.left = contentWidth * i + columnGap * i + "px";
  62.   
  63.           // 将行高push到数组
  64.           lineConentHeightList.push(height);
  65.         } else {
  66.           // 其他行
  67.           // 获取数组最小的高度 和 对应索引
  68.           let minHeight = Math.min(...lineConentHeightList);
  69.           let index = lineConentHeightList.findIndex(
  70.               (listH) => listH === minHeight
  71.           );
  72.   
  73.           contentList[i].style.top = minHeight + rowGap +"px";
  74.           contentList[i].style.left = (contentWidth + columnGap) * index + "px";
  75.   
  76.           // 修改最小列的高度 最小列的高度 = 当前自己的高度 + 拼接过来的高度 + 行间距
  77.           lineConentHeightList[index] += height + rowGap;
  78.         }
  79.       }
  80.     },
  81.   },
  82. };
  83. </script>
  84.   
  85. <style scoped>
  86. * {
  87.   margin: 0;
  88.   padding: 0;
  89. }
  90. /* 最外层大盒子 */
  91. .tab-container {
  92.   padding-top: 20px;
  93.   position: relative;
  94. }
  95. /* 每个小盒子 */
  96. .tab-container .tab-item {
  97.   position: absolute;
  98.   height: auto;
  99.   border: 1px solid #ccc;
  100.   box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  101.   background: white;
  102.   /* 元素不能中断显示 */
  103.   break-inside: avoid;
  104.   text-align: center;
  105. }
  106. .tab-container .tab-item img {
  107.   width: 100%;
  108.   height: auto;
  109.   display: block;
  110. }
  111. </style>


五.外层使用和懒加载

在使用这个插件的时候,有两个问题,就是因为内层是position: absolute;定位,不会撑开外部的p,会导致外层盒模型不好布局,还有就是页面下拉懒加载,那要怎么办呢?

这里我给出我的处理方法


手把手带你使用Vue实现一个图片水平瀑布流插件


整体代码如下:

  1. <template>
  2.   <div>
  3.     <div class="list-box" @scroll="scrollFun">
  4.       <compList :pbList="pbList" ref="compList"></compList>
  5.     </div>
  6.   </div>
  7. </template>
  8.   
  9. <script>
  10. import compList from "@/pages/test/components/compList";
  11. export default {
  12.   name:'testList',
  13.   components:{
  14.     compList
  15.   },
  16.   data() {
  17.     return {
  18.       //瀑布流数据
  19.       pbList: [
  20.         {
  21.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  22.         },
  23.         {
  24.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  25.         },
  26.         {
  27.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  28.         },
  29.         {
  30.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  31.         },
  32.         {
  33.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  34.         },
  35.         {
  36.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  37.         },
  38.         {
  39.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  40.         },
  41.         {
  42.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  43.         },
  44.         {
  45.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  46.         },
  47.         {
  48.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  49.         },
  50.         {
  51.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  52.         }
  53.       ],
  54.       addList:[
  55.         {
  56.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  57.         },
  58.         {
  59.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  60.         },
  61.         {
  62.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  63.         },
  64.         {
  65.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  66.         },
  67.         {
  68.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  69.         },
  70.         {
  71.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  72.         },
  73.         {
  74.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  75.         },
  76.         {
  77.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  78.         },
  79.         {
  80.           url: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fphoto%2Fm%2Fpublic%2Fp2650049201.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1664935370&t=d4bf3e4d352c277a1bdebfcc8fda959f",
  81.         },
  82.         {
  83.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  84.         },
  85.         {
  86.           url: "https://img1.baidu.com/it/u=2911909188,130959360&fm=253&fmt=auto&app=138&f=JPEG?w=440&h=641",
  87.         }
  88.       ],
  89.       bottomMain:true
  90.     };
  91.   },
  92.   methods:{
  93.     scrollFun(e) {
  94.       const  offsetHeight= e.target.offsetHeight
  95.       const  scrollHeight= e.target.scrollHeight
  96.       const  scrollTop= e.target.scrollTop
  97.       if((scrollHeight - (offsetHeight+scrollTop)) < 10){
  98.         if(this.bottomMain){
  99.           this.bottomMain = false
  100.           this.addListDataFun()
  101.         }
  102.       }
  103.     },
  104.     addListDataFun(){
  105.       this.$Spin.show({
  106.         render: (h) => {
  107.           return h('div', [
  108.             h('Icon', {
  109.               'class': 'demo-spin-icon-load',
  110.               props: {
  111.                 type: 'ios-loading',
  112.                 size: 18
  113.               }
  114.             }),
  115.             h('div', '数据更新中...')
  116.           ])
  117.         }
  118.       });
  119.       setTimeout(() => {
  120.         this.pbList = this.pbList.concat(this.addList)
  121.         this.bottomMain = true
  122.         this.$nextTick(()=>{
  123.           this.$refs.compList.waterFall("#tabContainer", ".tab-item")
  124.           this.$Spin.hide()
  125.         })
  126.       },1000)
  127.     }
  128.   }
  129. };
  130. </script>
  131.   
  132. <style scoped>
  133. .list-box{
  134.   position: relative;
  135.   width: 100%;
  136.   height: calc(100vh - 240px);
  137.   background: white;
  138.   padding: 20px 30px 20px 20px;
  139.   margin-top: 20px;
  140.   box-sizing: border-box;
  141.   overflow: auto;
  142. }
  143. </style>

下拉的核心代码为:

  1. scrollFun(e) {
  2.   const  offsetHeight= e.target.offsetHeight
  3.   const  scrollHeight= e.target.scrollHeight
  4.   const  scrollTop= e.target.scrollTop
  5.   if((scrollHeight - (offsetHeight+scrollTop)) < 10){
  6.     if(this.bottomMain){
  7.       this.bottomMain = false
  8.       this.addListDataFun()
  9.     }
  10.   }
  11. },
  12. addListDataFun(){
  13.   this.$Spin.show({
  14.     render: (h) => {
  15.       return h('div', [
  16.         h('Icon', {
  17.           'class': 'demo-spin-icon-load',
  18.           props: {
  19.             type: 'ios-loading',
  20.             size: 18
  21.           }
  22.         }),
  23.         h('div', '数据更新中...')
  24.       ])
  25.     }
  26.   });
  27.   setTimeout(() => {
  28.     this.pbList = this.pbList.concat(this.addList)
  29.     this.bottomMain = true
  30.     this.$nextTick(()=>{
  31.       this.$refs.compList.waterFall("#tabContainer", ".tab-item")
  32.       this.$Spin.hide()
  33.     })
  34.   },1000)
  35. }

这里使用的是iView-UI的全局加载事件,如果你要用其他的UI框架,也可以自行修改

到这里,所有的思路就结束了


本文网址:https://www.zztuku.com/detail-13183.html
站长图库 - 手把手带你使用Vue实现一个图片水平瀑布流插件
申明:本文转载于《博客园》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    4种移动端适配方法
    我们结婚吧PPT模板