小程序瀑布流案例

瀑布流

兼容小程序 ,h5 ,app

直接上代码

html代码
<template>
	<view class="goods-wrapper">
		<u-waterfall v-model="flowList" ref="uWaterfall">
			<template v-slot:left="{leftList}">
				<view class="goods-item" v-for="(item, index) in leftList" :key="index">
					<!-- 警告:微信小程序中需要hx2.8.11版本才支持在template中结合其他组件,比如下方的lazy-load组件 -->
					<u-lazy-load threshold="-450" :image="item.image" :index="index"></u-lazy-load>
					<!-- <u-image width="336" height="336" border-radius="16"></u-image> -->
					<viem class="goods-info">
						<text class="goods-name">2021 名师经典题</text>
						<text class="goods-desc">作业帮</text>
						<view class="goods-price">
							<text class="current-price"><text class="yen">&yen;</text>234</text>
							<text class="original-price"><text class="yen">&yen;</text>590</text>
						</view>
					</viem>
				</view>
			</template>
			<template v-slot:right="{rightList}">
				<view class="goods-item" v-for="(item, index) in rightList" :key="index">
					<u-lazy-load threshold="-450" :image="item.image" :index="index"></u-lazy-load>
					<!-- <u-image width="336" height="336" border-radius="16"></u-image> -->
					<viem class="goods-info">
						<text class="goods-name">2021 名师经典题</text>
						<text class="goods-desc">作业帮</text>
						<view class="goods-price">
							<text class="current-price"><text class="yen">&yen;</text>234</text>
							<text class="original-price"><text class="yen">&yen;</text>590</text>
						</view>
					</viem>
				</view>
			</template>
		</u-waterfall>
		<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
	</view>
</template>
js 代码
<script>
	export default {
		data() {
			return {
				loadStatus: 'loadmore',
				flowList: [],
				list: [{
						price: 35,
						title: '北国风光,千里冰封,万里雪飘',
						shop: '李白杜甫白居易旗舰店',
						image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23327_s.jpg',
					},
					{
						price: 75,
						title: '望长城内外,惟余莽莽',
						shop: '李白杜甫白居易旗舰店',
						image: 'http://pic.sc.chinaz.com/Files/pic/pic9/202002/zzpic23325_s.jpg',
					},
					{
						price: 385,
						title: '大河上下,顿失滔滔',
						shop: '李白杜甫白居易旗舰店',
						image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2119_s.jpg',
					},
					{
						price: 784,
						title: '欲与天公试比高',
						shop: '李白杜甫白居易旗舰店',
						image: 'http://pic2.sc.chinaz.com/Files/pic/pic9/202002/zzpic23369_s.jpg',
					}
				]
			}
		},
		onLoad() {
			this.addRandomData();
		},
		onReachBottom() {
			this.loadStatus = 'loading';
			// 模拟数据加载
			setTimeout(() => {
				this.addRandomData();
				this.loadStatus = 'loadmore';
			}, 1000)
		},
		methods: {
			addRandomData() {
				for (let i = 0; i < 10; i++) {
					let index = this.$u.random(0, this.list.length - 1);
					// 先转成字符串再转成对象,避免数组对象引用导致数据混乱
					let item = JSON.parse(JSON.stringify(this.list[index]))
					item.id = this.$u.guid();
					this.flowList.push(item);
				}
			}
		}
	}
</script>
scss代码
<style scoped lang="scss">
	.goods-wrapper {
		background: #f7f7f7;
	}

	.goods-item {
		display: flex;
		margin: 26rpx;
		flex-direction: column;
		background: #fff;
		border-radius: 16rpx;
		overflow: hidden;

		.goods-info {
			display: flex;
			flex-direction: column;
			padding: 20rpx;
		}

		.goods-name {
			margin: 20rpx 0;
			font-size: 28rpx;
			font-weight: bold;
		}

		.goods-desc {
			font-size: 24rpx;
			color: #808080;
			margin-bottom: 30rpx;
		}

		.goods-price {
			display: flex;

			.yen {
				font-size: 20rpx;
			}

			.current-price {
				color: #FF526A;
			}

			.original-price {
				margin-left: 30rpx;
				color: #B3B3B3;
				text-decoration: line-through;
			}
		}
	}
</style>
这里是效果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您提供一个微信小程序瀑布流布局的案例。以下是一个简单的示例代码: ```html <view class="waterfall"> <view class="column"> <view wx:for="{{column1}}" wx:key="index" class="item"> <!-- 这里是瀑布流的每个项目 --> </view> </view> <view class="column"> <view wx:for="{{column2}}" wx:key="index" class="item"> <!-- 这里是瀑布流的每个项目 --> </view> </view> <view class="column"> <view wx:for="{{column3}}" wx:key="index" class="item"> <!-- 这里是瀑布流的每个项目 --> </view> </view> </view> ``` ```javascript Page({ data: { column1: [], column2: [], column3: [], }, onLoad: function() { // 模拟数据,这里可以替换成您自己的数据源 const data = [ // 数据项 ]; // 计算每列的高度 let column1 = []; let column2 = []; let column3 = []; let height1 = 0; let height2 = 0; let height3 = 0; data.forEach((item) => { // 计算每个项目的高度,这里可以根据您的需求进行调整 const height = item.height; // 将项目添加到最短的列 if (height1 <= height2 && height1 <= height3) { column1.push(item); height1 += height; } else if (height2 <= height1 && height2 <= height3) { column2.push(item); height2 += height; } else { column3.push(item); height3 += height; } }); // 更新数据 this.setData({ column1, column2, column3, }); }, }); ``` 请注意,这只是一个简单的示例,您可能需要根据自己的需求进行相应的修改和优化。此示例将数据按照高度分配到三列,并在视图进行展示。您可以根据实际情况调整每个项目的高度和数据源。同时,您还可以添加滚动到底部加载更多数据的功能。希望对您有所帮助!如有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值