uniapp瀑布流实现(商品/图片瀑布流)

      瀑布流布局是一种以流式展示内容的方式,使内容以类似瀑布流一样的形式逐个排列,这种布局方式在网站中广泛应用,特别是在图片和商城类型上。

1. 图片瀑布流:


不依赖任何插件,复制即可见效

<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>


<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",
          
        ], //所有图片
        leftList: [], //左边列图片
        rightList: [], //右边列图片
        leftHeight: 0, //左边列高度
        rightHeight: 0, //右边列高度
        columnWidth: 0 //列宽度
      }
    },
    mounted() {
      this.$nextTick(() => {
        uni.createSelectorQuery().in(this).select('.left').boundingClientRect(res => {
          this.columnWidth = res.width
          //方法
          this.setWaterFallLayout()
        }).exec()
      })
    },
    
    onReachBottom() {
      console.log("触底");
      let newList = [
        "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=833730514,666835&fm=253&fmt=auto&app=138&f=JPEG?w=355&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=3311811998,3185175032&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=625",
        "https://img0.baidu.com/it/u=3319997766,4089593231&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
      ]
      this.imageList.push(...newList)
      console.log(this.imageList);
      this.setWaterFallLayout();
    },
    
    methods: {
      //通过uni.getImageInfo,小程序支持
      async setWaterFallLayout() {
        for (let item of this.imageList) {
          try {
            let h = await this.getImgHeight(item)
            if (this.leftHeight <= this.rightHeight) {
              this.leftList.push(item)
              this.leftHeight += h
            } else {
              this.rightList.push(item)
              this.rightHeight += h
            }

          } catch (e) {}

        }

      },
      getImgHeight(url) {
        return new Promise((resolve, reject) => {
          uni.getImageInfo({
            src: url,
            success: e => {
              resolve(e.height)

            },
            fail: e => {
              reject(e)
            }
          })

        })
      }
    }
  }
</script>


<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;
  }
  .info{
    width: 100%;
    height: 200rpx;
    background-color: #55aa7f;
  }
</style>

运行效果:

2. 商品瀑布流: 


不依赖任何插件,复制即可见效 

<template>
  <view class="Index">
    <!-- 瀑布流布局列表 -->
    <view class="pubuBox">
      <view class="pubuItem">
        <view class="item-masonry" v-for="(item, index) in comList" :key="index">
          <image :src="item.img" mode="widthFix"></image>
          <view class="listtitle">
            <!-- 这是没有高度的父盒子(下半部分) -->
            <view class="listtitle1">{{ item.name }}</view>
            <view class="listtitle2">
              <text class="listtitle2son">¥</text>
              {{ item.commdityPrice }}
            </view>
            <view class="listtitle3">
              某某某某旗舰店
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        comList: [{
            img: "https://img2.baidu.com/it/u=3853345508,384760633&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1689958800&t=210689b7eb06d7c78958d7063151cba6",
            name: '商品的名称,可以很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长',
            commdityPrice: 100
          }, {
            img: 'https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1689958800&t=d6764859a9740bb4aead7703daa61876',
            name: '商品名称会在超出两行时候自动折叠',
            commdityPrice: 100
          },
          {
            img: 'https://img0.baidu.com/it/u=1604010673,2427861166&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1689958800&t=2e255208580c0570167a800344d3aa59',
            name: '只有一行标题时高度为39',
            commdityPrice: 100
          }, {
            img: 'https://img0.baidu.com/it/u=2627496060,1933351908&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1689958800&t=fd7a89ad586d338543b6916df7083e4f',
            name: '具体样式您可以自定义',
            commdityPrice: 100
          }, {
            img: 'https://img2.baidu.com/it/u=3853345508,384760633&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1689958800&t=210689b7eb06d7c78958d7063151cba6',
            name: 'vue的H5页面也是如此使用',
            commdityPrice: 100
          }
        ], //商品列表
      };
    },
    onShow() {},
    onLoad() {},
    methods: {},
    // 触底加载下一页 换成真实的请求到的数据
    onReachBottom() {
      console.log("触底");
      // 模拟的请求数据
      let newList = [
        {
          img: "https://img2.baidu.com/it/u=1893470775,4143435497&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",
          name: '具体样式您可以自定义',
          commdityPrice: 100
        }, {
          img: "https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
          name: '具体样式您可以自定义',
          commdityPrice: 100
        }, {
          img: "https://img1.baidu.com/it/u=833730514,666835&fm=253&fmt=auto&app=138&f=JPEG?w=355&h=500",
          name: '具体样式您可以自定义',
          commdityPrice: 100
        }, {
          img: "https://img0.baidu.com/it/u=1088754973,1390499664&fm=253&fmt=auto&app=138&f=JPEG?w=335&h=500",
          name: '具体样式您可以自定义',
          commdityPrice: 100
        },
      ]
      this.comList.push(...newList)
      console.log(this.comList);
    },
  };
</script>

<style scoped="scoped" lang="scss">
  //瀑布流
  page {
    background-color: #eee;
    height: 100%;
  }

  .pubuBox {
    padding: 22rpx;
  }

  .pubuItem {
    column-count: 2;
    column-gap: 20rpx;
  }

  .item-masonry {
    box-sizing: border-box;
    border-radius: 15rpx;
    overflow: hidden;
    background-color: #fff;
    break-inside: avoid;
    /*避免在元素内部插入分页符*/
    box-sizing: border-box;
    margin-bottom: 20rpx;
    box-shadow: 0px 0px 28rpx 1rpx rgba(78, 101, 153, 0.14);
  }

  .item-masonry image {
    width: 100%;
  }

  .listtitle {
    padding-left: 22rpx;
    font-size: 24rpx;
    padding-bottom: 22rpx;

    .listtitle1 {
      line-height: 39rpx;
      text-overflow: -o-ellipsis-lastline;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      line-clamp: 2;
      -webkit-box-orient: vertical;
      min-height: 39rpx;
      max-height: 78rpx;
    }

    .listtitle2 {
      color: #ff0000;
      font-size: 32rpx;
      line-height: 32rpx;
      font-weight: bold;
      padding-top: 22rpx;

      .listtitle2son {
        font-size: 32rpx;
      }
    }

    .listtitle3 {
      font-size: 28rpx;
      color: #909399;
      line-height: 32rpx;
      padding-top: 22rpx;
    }
  }

  .Index {
    width: 100%;
    height: 100%;
  }
</style>

运行效果:

瀑布流的一些要点: 


  1. 布局结构:Web瀑布流布局通过动态加载和排列内容,使得页面更加有吸引力和互动性。它使用CSS和JavaScript来实现自适应布局,确保在不同尺寸的屏幕上都能达到最佳效果。

  2. 图片加载:在Web瀑布流中,图片通常是主要的内容元素。为了加快页面加载速度,常常使用延迟加载技术,即只在用户滚动到可见区域时才加载图片。这种方法可以有效减少页面加载时间。

  3. 动态排列:Web瀑布流通过JavaScript实现了动态排列内容的功能。它根据内容的高度和宽度等因素来决定每个内容块的位置,以使得页面整体看起来更加美观。需要注意的是,动态排列需要通过计算和调整来确保内容块不会重叠或出现缺口。

  4. 响应式设计:Web瀑布流布局在不同设备上都能够适应,包括桌面、平板和移动设备。采用响应式设计可以根据屏幕尺寸和方向来动态调整内容的排列和展示方式,提供更好的用户体验。

 

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Uniapp 中可以使用 `u-grid` 组件实现瀑布流布局。以下是一个简单的例子: 1. 在页面中引入 `u-grid` 组件: ```html <template> <u-grid :list="images" :column-num="2"> <template v-slot="{ item }"> <img :src="item.url" style="width: 100%;" /> </template> </u-grid> </template> ``` 2. 在页面中定义图片列表数据: ```js <script> export default { data() { return { images: [ { url: 'https://picsum.photos/id/1/300/200' }, { url: 'https://picsum.photos/id/2/300/400' }, { url: 'https://picsum.photos/id/3/300/200' }, { url: 'https://picsum.photos/id/4/300/500' }, { url: 'https://picsum.photos/id/5/300/200' }, { url: 'https://picsum.photos/id/6/300/300' }, { url: 'https://picsum.photos/id/7/300/400' }, { url: 'https://picsum.photos/id/8/300/200' }, { url: 'https://picsum.photos/id/9/300/600' }, { url: 'https://picsum.photos/id/10/300/200' }, { url: 'https://picsum.photos/id/11/300/400' }, { url: 'https://picsum.photos/id/12/300/200' }, { url: 'https://picsum.photos/id/13/300/300' }, { url: 'https://picsum.photos/id/14/300/400' }, { url: 'https://picsum.photos/id/15/300/200' }, { url: 'https://picsum.photos/id/16/300/500' }, { url: 'https://picsum.photos/id/17/300/200' }, { url: 'https://picsum.photos/id/18/300/300' }, { url: 'https://picsum.photos/id/19/300/400' }, { url: 'https://picsum.photos/id/20/300/200' } ] } } } </script> ``` 这样就可以实现一个简单的瀑布流布局。你可以根据需要调整列数和图片数据。注意,`u-grid` 组件需要引入 `uview-ui` 库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

耀南.

你的鼓励将是我最最最最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值