最簡(jiǎn)單的微信小程序瀑布流布局方法

最近寫(xiě)一個(gè)小程序的瀑布流布局,發(fā)現(xiàn)網(wǎng)上別人寫(xiě)的都是css加js的方法,頁(yè)面分兩列,加載數(shù)據(jù)通過(guò)JS分別載入到兩列中。比較復(fù)雜,研究了一下,弄了一個(gè)比較簡(jiǎn)單的方法,分享給大家。
效果如下圖:


小程序瀑布流布局

wxml頁(yè)面

<view class='case-page'>
  <view class='list-masonry'>
    <view class='item-masonry' wx:for="{{note}}">
      <image src='{{item.url}}' mode='widthFix'></image>
      <text>{{item.title}}</text>
    </view>
  </view>
</view>

wxss

page{
  background-color: #eee;
}
.case-page{
  padding:20rpx;
}
.list-masonry{
  column-count: 2;
  column-gap: 20rpx;
}
.item-masonry{
  background-color: #fff;break-inside: avoid; box-sizing: border-box; padding: 20rpx;
}
.item-masonry image {
  width: 100%;
}

js頁(yè)面給一個(gè)初始數(shù)據(jù)

Page({
  /**
   * 頁(yè)面的初始數(shù)據(jù)
   */
  data: {
    imgWidth:0,imgHeight:0,
    note: [
      {
        title: '案例名稱(chēng)',
        url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_176CFE51B6710715B1BBBEF2F86ACB0C.jpg',
      }, 
      {
        title: '你所不知道的紅酒知識(shí)',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg',
      }, 
      {
        title: '紅酒知識(shí)',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72',
      },
      {
        title: '案例名稱(chēng)',
        url: 'http://zq.jhcms.cn/attachs/photo/201711/20171130_9E39DA252E3946BE36218D85876C4AB4.jpg',
      },
      {
        title: '案例名稱(chēng)',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg'
      },
       
      {
        title: '案例名稱(chēng)',
        url: 'http://f10.baidu.com/it/u=121654667,1482133440&fm=72'
      },
      {
        title: '案例名稱(chēng)',
        url: 'http://img4.imgtn.bdimg.com/it/u=2748975304,2710656664&fm=26&gp=0.jpg'
      }, 
      {
        title: '案例名稱(chēng)',
        url: 'http://img2.imgtn.bdimg.com/it/u=1561660534,130168102&fm=26&gp=0.jpg'
      },
      {
        title: '案例名稱(chēng)',
        url: 'http://img3.imgtn.bdimg.com/it/u=1417732605,3777474040&fm=26&gp=0.jpg'
      }
    ]
  }
})

基本就是html+css3實(shí)現(xiàn)的瀑布流布局。上拉加載還沒(méi)有寫(xiě)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容