uni-app实现瀑布流效果,支持上拉加载更多(修改后可应用于小程序)

这是一个绝对定位方式的瀑布流实现,直接贴上代码了

<template>
	<view class="content">
		<view hidden>
			<block v-for="(a,b) in imgList">
				<block v-for="(item,index) in a">
					<image @load="imageLoad" :data-src="item.src" :src="item.src" mode="widthFix"></image>
				</block>
			</block>
		</view>
		<view>
			<block v-for="(item,index) in imgList1">
				<image :src="item.src" mode="widthFix" :style="`left: ${item.left}upx;top: ${item.top}upx;`"></image>
			</block>
		</view>
	</view>
</template>

<script>
	var colCount //定义列数
	var colHeightArry = [] //定义列高度数组
	var imgWidth = 375 //单张图片的宽度,可通过调整宽度显示不同列数

	colCount = parseInt(750 / imgWidth) //计算出列数,这里是两列

	for (var i = 0; i < colCount; i++) {
		colHeightArry[i] = 0
	}

	export default {
		data() {
			return {
				title: 'Hello',
				imgList: [
					[{
						src: "../../static/1.jpg"
					}, {
						src: "../../static/2.jpg"
					}, {
						src: "../../static/david-travis-554904-unsplash.jpg"
					}, {
						src: "../../static/3.jpg"
					}, {
						src: "../../static/4.jpg"
					}, {
						src: "../../static/5.jpg"
					}, {
						src: "../../static/6.jpg"
					}, {
						src: "../../static/7.jpg"
					}, {
						src: "../../static/8.jpg"
					}, {
						src: "../../static/9.jpg"
					}, {
						src: "../../static/10.jpg"
					}, {
						src: "../../static/11.jpg"
					}, {
						src: "../../static/12.jpg"
					}, {
						src: "../../static/13.jpg"
					}, {
						src: "../../static/14.jpg"
					}, {
						src: "../../static/15.jpg"
					}, {
						src: "../../static/16.jpg"
					}, {
						src: "../../static/17.jpg"
					}, {
						src: "../../static/1569568584_xcx2.jpg"
					}, {
						src: "../../static/18.jpg"
					}, {
						src: "../../static/19.jpg"
					}, {
						src: "../../static/20.jpg"
					}]
				],
				imgList1: []
			}
		},
		onLoad() {

		},
		onReachBottom() {
			var imgList = [{
				src: "../../static/1.jpg"
			}, {
				src: "../../static/2.jpg"
			}, {
				src: "../../static/david-travis-554904-unsplash.jpg"
			}, {
				src: "../../static/3.jpg"
			}, {
				src: "../../static/4.jpg"
			}, {
				src: "../../static/5.jpg"
			}, {
				src: "../../static/6.jpg"
			}, {
				src: "../../static/7.jpg"
			}, {
				src: "../../static/8.jpg"
			}, {
				src: "../../static/9.jpg"
			}, {
				src: "../../static/10.jpg"
			}, {
				src: "../../static/11.jpg"
			}, {
				src: "../../static/12.jpg"
			}, {
				src: "../../static/13.jpg"
			}, {
				src: "../../static/14.jpg"
			}, {
				src: "../../static/15.jpg"
			}, {
				src: "../../static/16.jpg"
			}, {
				src: "../../static/17.jpg"
			}, {
				src: "../../static/1569568584_xcx2.jpg"
			}, {
				src: "../../static/18.jpg"
			}, {
				src: "../../static/19.jpg"
			}, {
				src: "../../static/20.jpg"
			}]
			this.imgList.push(imgList)
		},
		methods: {
			imageLoad(e) {
				var src = e.currentTarget.dataset.src //图片地址
				var width = e.detail.width //图片宽度
				var height = e.detail.height //图片高度
				height = imgWidth * height / width //在设备上实际显示的高度
				var minValue = colHeightArry[0] //定义最小的高度
				var minIndex = 0 //定义最小高度的下标
				for (var i = 0; i < colCount; i++) {
					if (colHeightArry[i] < minValue) { //如果最小高度组数中的值小于最小值
						minValue = colHeightArry[i] //那么认为最小高度数组中的值是真正的最小值
						minIndex = i //最小下标为当前下标
					}
				}
				this.imgList1.push({
					src: src,
					left: minIndex * imgWidth,
					top: minValue,
					height: height
				})
				colHeightArry[minIndex] += height
			}
		}
	}
</script>

<style>
	image {
		position: absolute;
		width: 355upx;
		margin: 10upx;
	}
</style>
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
uni-app中,上拉加载更多是一种见的交互方式,用于在列表或页面中加载更多的数据。下面是一种实现上拉加载更多的方法: 1. 首先,在你的页面或组件中,需要监听页面滚动事件,以便判断用户是否已经滚动到了页面底部。 2. 当用户滚动到页面底部时,触发一个加载更多的函数。 3. 在加载更多的函数中,可以发送异步请求获取更多的数据。 4. 获取到数据后,将新数据追加到原有的数据列表中。 5. 更新页面的数据绑定,使页面能够展示新加载的数据。 下面是一个示例代码: ```html <template> <view> <!-- 列表展示数据 --> <view v-for="(item, index) in dataList" :key="index">{{ item }}</view> <!-- 加载更多提示 --> <view v-if="loading">加载中...</view> </view> </template> <script> export default { data() { return { dataList: [], // 存储数据列表 loading: false, // 是否正在加载更多 page: 1, // 当前页码 pageSize: 10, // 每页数据数量 }; }, mounted() { // 监听页面滚动事件 uni.pageScrollTo({ scrollTop: 0, duration: 0, }); uni.onPageScroll(this.handlePageScroll); }, destroyed() { // 取消监听页面滚动事件 uni.offPageScroll(this.handlePageScroll); }, methods: { handlePageScroll(e) { // 判断是否滚动到页面底部 const { scrollTop, scrollHeight, windowHeight } = e; if (scrollTop + windowHeight >= scrollHeight) { this.loadMore(); } }, loadMore() { // 正在加载中,避免重复触发 if (this.loading) return; this.loading = true; // 发送异步请求获取更多数据 uni.request({ url: 'your-api-url', data: { page: this.page, pageSize: this.pageSize, }, success: (res) => { // 获取到新数据 const newData = res.data; // 将新数据追加到原有数据列表中 this.dataList = this.dataList.concat(newData); // 更新页码 this.page++; // 加载完成,重置loading状态 this.loading = false; }, fail: (err) => { console.error(err); // 加载失败,重置loading状态 this.loading = false; }, }); }, }, }; </script> ``` 这是一个简单的示例,你可以根据自己的实际需求进行修改和扩展。希望对你有所帮助!
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值