欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

uniapp自定義多列瀑布流組件項目實戰(zhàn)總結

 更新時間:2023年09月20日 09:23:53   作者:MarkGuan  
這篇文章主要為大家介紹了uniapp自定義多列瀑布流組件實戰(zhàn)總結,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

導語

有時候展示圖片等內容,會遇到圖片高度不一致的情況,這時候就不能使用等高雙列或多列展示了,這時候會用到瀑布流的頁面布局,下面就一起探討一下瀑布流的實現(xiàn)方法。

準備工作

  • pages/index文件夾下面新建一個waterfall.vue的組件;
  • 按照前面文章所說的頁面結構,編寫好預定的瀑布流案例頁面;
  • 在網上找?guī)讖埫赓M的圖片素材;

原理分析

主要是根據圖片的高度自動比較每列的總高度,根據uni.getImageInfo獲取到圖片高度,然后哪列低,就給哪列補充圖片,直至圖片列表循環(huán)完畢。

實戰(zhàn)演練

模板使用

下面就是循環(huán)列和圖片。

<view class="waterfall-page">
  <view
    class="waterfall-page-column"
    v-for="(item, index) in waterfall.columnList"
    :key="index"
    ref="column"
  >
    <view
      class="waterfall-page-item"
      v-for="(pItem, pIndex) in item"
      :key="pIndex"
    >
      <image class="waterfall-page-img" :src="pItem" mode="widthFix"></image>
    </view>
  </view>
</view>

樣式編寫

.waterfall-page {
  display: flex;
  align-items: flex-start;
  .waterfall-page-column {
    box-sizing: border-box;
    flex: 1;
    padding: 0 10rpx;
    width: 0;
    .waterfall-page-item {
      margin-bottom: 10rpx;
      .waterfall-page-img {
        display: inline-block;
        width: 100%;
      }
    }
  }
}

腳本使用

  • 定義數據
// 瀑布流信息
const waterfall = reactive({
  // 圖片列表
  imgList: [
    "/static/image/waterfall/pic-01.jpg",
    "/static/image/waterfall/pic-07.jpg",
    "/static/image/waterfall/pic-03.jpg",
    "/static/image/waterfall/pic-07.jpg",
    "/static/image/waterfall/pic-05.jpg",
    "/static/image/waterfall/pic-07.jpg",
    "/static/image/waterfall/pic-01.jpg",
    "/static/image/waterfall/pic-03.jpg",
    "/static/image/waterfall/pic-07.jpg",
  ],
  columnList: [], // 每列圖片
  columnHeight: [], // 每列圖片高度
  columnCount: 2, // 總列數
});
  • 初始化數據
// 初始化數據
function initData() {
  for (var i = 0; i < waterfall.columnCount; i++) {
    waterfall.columnList.push([]);
    waterfall.columnHeight.push(0);
  }
}
  • 計算方法
// 設置瀑布流布局
async function setWaterfallLayout() {
  for (var i = 0; i < waterfall.imgList.length; i++) {
    let item = waterfall.imgList[i];
    try {
      let imgInfo = await uni.getImageInfo({
          src: item,
        }),
        h = imgInfo.height;
      for (let j = 0; j < waterfall.columnCount - 1; j++) {
        let prevIndex = j,
          nextIndex = j + 1;
        if (
          waterfall.columnHeight[prevIndex] <= waterfall.columnHeight[nextIndex]
        ) {
          waterfall.columnList[prevIndex].push(item);
          waterfall.columnHeight[prevIndex] += h;
        } else {
          waterfall.columnList[nextIndex].push(item);
          waterfall.columnHeight[nextIndex] += h;
        }
      }
    } catch (error) {
      console.log(error);
    }
  }
}

案例展示

h5 端效果

小程序端效果

APP 端效果

以上就是uniapp自定義多列瀑布流組件實戰(zhàn)總結的詳細內容,更多關于uniapp多列瀑布流組件的資料請關注腳本之家其它相關文章!

相關文章

最新評論