uniapp 小程序商品图片实现瀑布流效果

8436b1789cc94454a2efa40853272049.png

2eee5acd783c45509b1eda545c03a77a.png

46addc591145457a9bee69725b8ad421.png

c2259ec8bee640f69e113d84765e4e64.png

32548a0f97a54a0e9aa7e980f5598c0b.png

<view class="imageContainer">
			<view class="imageItem" v-for="(item, index) in columnData" :key="index">
				<view v-for="(childItem, childIndex) in item" :key="childIndex" style="width: 100%"
					:id="'item' + childItem._id" @click.stop="click(childItem)" class="deepItem">
					<view class="img" style="margin-bottom: 5rpx;">
						<image :src="childItem.img" class="smallImage"></image>
					</view>
					<view style="font-weight: bold; margin-bottom: 10rpx;font-size: 30rpx;">
						{{childItem.name}}
					</view>
					<view
						style="color: #C0C0C0;font-size: 24rpx; margin-bottom: 15rpx;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
						{{childItem.name_pw}}
					</view>
					<view class="startFlex">
						<view class="littleDeliverItem">
							<text style="color:#50AA62;font-size: 18rpx;">到店自提</text>
						</view>

						<view class="littleDeliverItem">
							<text style="color:#FF6A6C;font-size: 18rpx;">快递到家</text>
						</view>
					</view>

					<view class="betweenFlex">
						<view class="left">
							<text style="color: #FF6A6C;">¥</text>
							<text
								style="color: #FF6A6C;font-size:40rpx;font-weight: bold;margin-right: 10rpx;">{{childItem.price}}</text>
							<text
								style="color: #737373;text-decoration:line-through;">¥{{childItem.market_price}}</text>
						</view>
						<view class="right plusRightContainer">
							<uni-icons type="plusempty" size="16" style="font-weight: bold;"></uni-icons>
						</view>
					</view>
				</view>

			</view>
		</view>
       data() {
			return {
				goodsList: [],
				column: 2,
				columnData: [],
				columnWidth: 0,
			};
		},


    methods:{

        
//计算每列的高度
			getElemHeight(index) {
				this.$nextTick(() => {
					var arr = [];
					this.goodsList.map((item, index) => {
						uni.getImageInfo({
							src: item.img,
							success: (e) => {
								item.height = (e.height * (this.columnWidth / e.width)) * 2 +
									'rpx'
								uni.createSelectorQuery().select('#item' + item.id)
									.boundingClientRect(res => {
										if (res !== null) {
											arr.push({
												...{
													itemHeight: res.height
												},
												...item
											});
											if (arr.length == this.goodsList.length) {
												this.columnData = this.distributeToNArrays(
													arr,
													this.column);

											}
										}
									}).exec();
							}
						})
					})
				})
			},
			distributeToNArrays(arr, n) {
				let sums = new Array(n).fill(0);
				return arr.reduce(
					(arrays, item) => {
						let minSum = Math.min(...sums);
						let minIndex = sums.indexOf(minSum);
						arrays[minIndex].push(item);
						sums[minIndex] += item.itemHeight;
						return arrays;
					},
					new Array(n).fill().map(() => []),
				)
			},
			setColumnWidth() {
				let width = uni.getSystemInfoSync().windowWidth
				this.columnWidth = Math.floor(width / this.column)
			},
			setData() {
				const resultArray = this.goodsList.reduce(
					(acc, cur, index) => {
						const targetIndex = index % this.column;
						acc[targetIndex].push(cur);
						return acc;
					},
					Array.from(Array(this.column), () => []),
				);
				this.columnData = resultArray
				this.getElemHeight()
			},
			click(item) {
				console.log(item)
				uni.navigateTo({
					url:"/subPackage/goodDetail/goodDetail",
				})
			},





    }

来看效果图

7447871a9281415691cd74c0a6b43458.png

 d5681e6bf6154bf2b6314f2e94af0118.png

fc0718ba072e46498fbfa982b51774fe.png

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值