当前位置: 首页 > news >正文

uniapp 封装瀑布流组件

思路:

1.coulumns:需要分成几列

2.如何分布数据

3.计算每列的宽度

4.图片进行高度自适应

<template><view :style="{ margin: boxM }"><view class="flex flex-justify-start bg-red" style="background-color: #e8e0e0; border-radius: 20rpx; padding: 20rpx"><viewclass="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: stringitemGap: stringcolumnCount: numberdata: 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().windowWidthwidth.value = ((screenWidth - 40 - (columnCount.value - 1) * parseInt(itemGap.value)) / columnCount.value) * (1 / screenRate) + 'rpx'for (let i = 0; i < columnCount.value; i++) {list.value[i] = []}let i = 0data.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>

http://www.lryc.cn/news/396761.html

相关文章:

  • pd虚拟机去虚拟化是什么意思?pd虚拟机去虚拟化教程 PD虚拟机优化设置
  • 低代码研发项目管理流程优化:提效与创新的双重驱动
  • 32位版 C 库函数time 将在 2038 年溢出,那到时候,它该何去何从
  • C语言 printf函数缓冲机制
  • 【Linux进阶】文件系统8——硬链接和符号连接:ln
  • 代码随想录算法训练营Day64|拓扑排序(卡码网117)、dijkstra朴素版
  • neo4j 图数据库:Cypher 查询语言、医学知识图谱
  • 数据结构基础--------【二叉树基础】
  • 数据开源 | Magic Data大模型高质量十万轮对话数据集
  • webpack之ts打包
  • MATLAB数据统计描述和分析
  • 设计分享—国外后台界面设计赏析
  • 最小生成树(算法篇)
  • 教师管理小程序的设计
  • Selenium 等待
  • 安装easy-handeye
  • 【面试题】MySQL 索引(第二篇)
  • 4. 小迪安全v2023笔记 javaEE应用
  • anaconda修改安装的默认环境
  • MySQL 9.0 正式发行Innovation创新版已支持向量
  • 基于Java+SpringMvc+Vue技术的智慧校园系统设计与实现
  • 【蔬菜网元宇宙】—— 探索农业的未来之旅
  • 淘宝商品历史价格查询(免费)
  • 14-47 剑和诗人21 - 2024年如何打造AI创业公司
  • WPF界面设计-更改按钮样式 自定义字体图标
  • 开源项目的机遇与挑战
  • Linux实现CPU物理隔离
  • springer latex模板参考文献不显示
  • 使用Vue3、Pinia和Vite5打造高度还原的抖音仿制项目
  • stm32基本定时器