uniapp 封装瀑布流组件

思路:

1.coulumns:需要分成几列

2.如何分布数据

3.计算每列的宽度

4.图片进行高度自适应

<template>
  <view :style="{ margin: boxM }">
    <view class="flex flex-justify-start bg-red" style="background-color: transparent; border-radius: 20rpx">
      <view
        class="waterfall-list"
        v-for="(item, index) in columnCount"
        :key="index"
        :style="{ width: width, margin: `0 ${itemGap}`, borderRadius: '20rpx' }"
      >
        <view v-for="(el, i) in list[index]" :key="i" class="waterfall-item">
          <slot name="content" :item="el" />
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { getSysInfo } from '@/utils/permission'
type Props = {
  sideGap: string
  itemGap: string
  columnCount: number
  data: Array<any>
}
const props = defineProps<Props>()
let width = ref('')
const { screenRate } = getApp().globalData
const boxM = computed(() => {
  return props.sideGap || '20rpx'
})
const itemGap = computed(() => {
  return props.itemGap ? parseInt(props.itemGap) / 2 + 'rpx' : '10rpx'
})
const columnCount = computed(() => {
  return props.columnCount || 2
})
const data = computed(() => {
  return props.data || []
})
const list = ref<any>([])
onMounted(() => {
  let screenWidth = getSysInfo().windowWidth
  width.value = ((screenWidth - (columnCount.value - 1) * parseInt(itemGap.value)) / columnCount.value) * (1 / screenRate) + 'rpx'
  for (let i = 0; i < columnCount.value; i++) {
    list.value[i] = []
  }
  let i = 0
  data.value.forEach((el, index) => {
    if (i > columnCount.value - 1) {
      i = 0
    }
    list.value[i].push(el)
    i++
  })
})
</script>

<style scoped lang="scss">
.waterfall-list {
  &:first-child {
    margin-left: 0 !important;
  }

  &:last-child {
    margin-right: 0 !important;
  }

  .waterfall-item {
    border-radius: 20rpx;
    margin-bottom: 20rpx;
  }
}
</style>

<!-- 使用 -->
<water-fall :columnCount="2" :data="arr">
      <template v-slot:content="{ item }">
        <view class="" style="">
          <image :src="item.url" style="width: 100%; border-radius: 20rpx" mode="widthFix"></image>
          <view class="" style="text-align: center">123</view>
        </view>
      </template>
    </water-fall>

...

<script setup lang="ts">
import waterFall from '@/components/Waterfall'
const arr = [
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1716641663545-WechatIMG367.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' },
  { url: 'http://localhost:3000/uploadSubIcon/1718875685835-WechatIMG3260.png' }
]
</script>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Goat恶霸詹姆斯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值