微信小程序实现瀑布流

实现思路:

1.由于是左右两排照片而且左右照片每排的每个都有高度,需要先定义变量,

2.遍历数组,通过wx.getImageInfo获取到每个元素图片的宽度和高度(需要配置download的域名)

3.判断这个元素应该是放在左边还是右边(放在数组高度低的一边)

4.最后 this.setData,数据驱动视图,在前端渲染这些数据

5.别忘给image属性加上mode="widthFix",高度自适应,保持宽高比

附上代码

//在页面的js文件中
 data: {
    //用于接收请求回来的数组的数组
    list: [],
    //左边的数组
    leftList: [],
    //右边的数组
    rightList: [],
    //左边的高度
    leftHeight: 0,
    //右边的高度
    rightHeight: 0,
    //上一个数组处理完了还是没有处理完
    flag: false,
    //触底加载是否全部加载完成的变量
    isEnd: false,
    //定义的pageNum(页码数),是后台api给的query参数,
    pageNum: 1,
  },
  

//封装的函数

//将数组作为参数放进函数中
 init(list) {
    //刚开始falg是false
    this.data.flag = false
    if (list.length == 0) {
      //如果数组的长度为0就说明处理完了,
      this.data.flag = true
      //返回就不会往下走了
      return
    }
    //定义变量接收数组删除的第一个元素
    let arr = list.shift()
    //wx的处理图片的api,(可以自己去看文档了解一下)
    wx.getImageInfo({     
      src: arr.coverImgUrl,
      success: (res) => {
        //如果左边的高度小于等于右边的高度
        if (this.data.leftHeight <= this.data.rightHeight) {
          //左边的数组接收arr
          this.data.leftList.push(arr)
          //左边的高度去加上 getImageInfo 成功之后返回的数组的高度除以宽度,依靠这个数据去一个一个加起来,来判断加在哪边的数组
          this.data.leftHeight += res.height / res.width
        } else {
          this.data.rightList.push(arr)
          this.data.rightHeight += res.height / res.width
        }
        //更改视图
        this.setData({
          leftList: this.data.leftList,
          rightList: this.data.rightList
        })
      },
      complete: () => {
       //成功或者失败都会走的函数,把参数作为形参再次调用这个函数
        this.init(list)
      }
    })
  },

   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   //这里我是封装了一个请求,然后请求回来的数据
    postList(1).then(res => {
      console.log(res);
      this.init(res.data.rows)
    })
  },

  

我用的是递归的方法,如果要是改用循环的话可能还要复杂一些

用户下拉刷新

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.data.isEnd = false
    if (this.data.flag) {
      //如果请求回来的数组中的数据处理完了,这个flag就会变为false
      this.setData({
        //这里的下拉就相当于重新请求,变量都变为空
        leftHeight: 0,
        rightHeight: 0,
        leftList: [],
        rightList: [],
      })
      postList(1).then(res => {
        console.log(res);
        //重新调用函数
        this.init(res.data.rows)
      })
      //这个是页码变为1
      this.data.pageNum =1
    } else {
      return
    }
  },

上拉触底加载更多

  onReachBottom() { 
    //还是如果处理完上次请求的数组数据就运行
    if (this.data.flag) {
      //如果上拉请求的数据请求完了就变为true
      if (!this.data.isEnd) {
        //页码加1
        this.data.pageNum++
        postList(this.data.pageNum).then(res => {
          console.log(res);
          if (this.data.pageNum * 10 >= res.data.total) {
            console.log('最后一页');
            this.init(res.data.rows)
            this.setData({
              isEnd:true
            })
          }else{
            this.init(res.data.rows)
          }
        })
      }
    }
  },
   
  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现微信小程序瀑布布局的代码如下: ``` <!-- .wxml --> <scroll-view scroll-y="true" bindscrolltolower="loadMoreData"> <view class="waterfall"> <view class="waterfall-cell" wx:for="{{items}}" wx:key="{{item.id}}"> <image class="waterfall-img" src="{{item.imgUrl}}" mode="aspectFit"></image> <view class="waterfall-title">{{item.title}}</view> </view> </view> </scroll-view> /* .wxss */ .waterfall { display: flex; flex-wrap: wrap; justify-content: space-between; padding: 10rpx; } .waterfall-cell { width: 49%; margin-bottom: 10rpx; background-color: #fff; border-radius: 10rpx; box-shadow: 0 2rpx 10rpx rgba(0,0,0,.2); } .waterfall-img { width: 100%; height: 0; padding-bottom: 100%; border-radius: 10rpx 10rpx 0 0; } .waterfall-title { padding: 10rpx; font-size: 28rpx; color: #333; } /* .js */ Page({ data: { items: [ { id: 1, title: 'Item 1', imgUrl: 'item1.jpg' }, { id: 2, title: 'Item 2', imgUrl: 'item2.jpg' }, { id: 3, title: 'Item 3', imgUrl: 'item3.jpg' }, ... ] }, loadMoreData() { // TODO: 加载更多数据 } }) ``` 以上代码中,我们使用了 `scroll-view` 组件实现了垂直滚动,并且绑定了 `bindscrolltolower` 事件,当用户滑动到底部时会触发该事件。在 `scroll-view` 内部,使用了一个 `view` 容器包裹所有的瀑布单元格。每个瀑布单元格使用一个 `view` 组件实现,包含了一张图片和一个标题。在 CSS 样式中,我们使用了 Flex 布局实现瀑布布局,并设置了每个单元格的宽度、高度、margin等属性,并使用圆角和阴影使其更加美观。在 JS 代码中,我们使用了一个数组 `items` 存储了所有的数据,并且定义了一个 `loadMoreData` 函数,用于加载更多数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜鸟小宇要努力

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

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

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

打赏作者

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

抵扣说明:

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

余额充值