uniapp屏幕滚动,所处对应的页码随之改变

一、视频图片效果:

视频效果

二、代码实现:

HTML部分

<template>
	<view class="container">
		<view class="current">{{current}} / {{contractPhotoLength}}</view>
		<block v-for="(item,index) in contractPhoto" :key="item.id">
			<image class="img_model" :src="item.img" mode="" @click="clickPhoto">
			</image>
		</block>
	</view>
	<view class="btn">
		<view class="btn_txt" @click="creareContract">创建合同</view>
	</view>
</template>

JavaScript部分

<script>
	export default {
		data() {
			return {
				contractPhoto: [{
						id: 11,
						img: 'https://imgcdn.baogaoting.com/WordImage/2021-05-14/e84f65f40084461faff961e365c3f2ca.jpg'
					},
					{
						id: 22,
						img: 'https://imgcdn.baogaoting.com/WordImage/2021-05-14/e84f65f40084461faff961e365c3f2ca.jpg'
					},
					{
						id: 121,
						img: 'https://imgcdn.baogaoting.com/WordImage/2021-05-14/e84f65f40084461faff961e365c3f2ca.jpg'
					},
					{
						id: 111,
						img: 'https://imgcdn.baogaoting.com/WordImage/2021-05-14/e84f65f40084461faff961e365c3f2ca.jpg'
					},
					{
						id: 141,
						img: 'https://imgcdn.baogaoting.com/WordImage/2021-05-14/e84f65f40084461faff961e365c3f2ca.jpg'
					},
					{
						id: 145641,
						img: 'https://imgcdn.baogaoting.com/WordImage/2021-05-14/e84f65f40084461faff961e365c3f2ca.jpg'
					},
					{
						id: 1410,
						img: 'https://imgcdn.baogaoting.com/WordImage/2021-05-14/e84f65f40084461faff961e365c3f2ca.jpg'
					}
				],
				current: 1,
				contractPhotoLength: '',
				// 单张图片的高度
				onlyPhoto: '',
				// 单张图片的一半高度
				onlyPhotoHalf: ''
			}
		},
		onLoad() {
		},
		onReady() {
			let that = this
			// this.getContractHeight()
			let info = uni.createSelectorQuery().select(".container");
			info.boundingClientRect(function(data) {
				that.contractPhotoLength = that.contractPhoto.length;
				// 单张图片的高度
				let a = parseInt(data.height / that.contractPhotoLength)
				that.onlyPhoto = parseInt(data.height / that.contractPhotoLength)
				// 单张图片的一半高度
				let b = parseInt((data.height / that.contractPhotoLength) / 2)
				that.onlyPhotoHalf = parseInt((data.height / that.contractPhotoLength) / 2)
			}).exec()
		},
		onPageScroll(e) {
			let that = this
			const {
				onlyPhotoHalf,
				onlyPhoto
			} = this; // 从外部获取配置或提取相关值  
			let current = 1; // 初始值  

			for (let i = 0; i < that.contractPhotoLength; i++) { // 假设最多有5个分区  
				const scrollTopThreshold = onlyPhotoHalf + i * onlyPhoto;
				if (e.scrollTop > 0 && e.scrollTop <= scrollTopThreshold) {
					current = i + 1; // 设置当前分区编号  
					break; // 找到对应的分区后退出循环  
				}
			}

			this.current = current; // 设置当前值  
		},
		// onPageScroll(e) {
		// 	if (e.scrollTop > 0 && e.scrollTop < this.onlyPhotoHalf) {
		// 		this.current = 1
		// 	} else if (e.scrollTop > 0 && e.scrollTop < this.onlyPhotoHalf + this.onlyPhoto * 1) {
		// 		this.current = 2
		// 	} else if (e.scrollTop > 0 && e.scrollTop < this.onlyPhotoHalf + this.onlyPhoto * 2) {
		// 		this.current = 3
		// 	} else if (e.scrollTop > 0 && e.scrollTop < this.onlyPhotoHalf + this.onlyPhoto * 3) {
		// 		this.current = 4
		// 	} else if (e.scrollTop > 0 && e.scrollTop < this.onlyPhotoHalf + this.onlyPhoto * 4) {
		// 		this.current = 5
		// 	}
		// },
		methods: {
			clickPhoto() {
				let arr = []
				this.contractPhoto.forEach(item => {
					arr.push(item.img)
				})
				// 预览图片
				uni.previewImage({
					urls: arr,
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {},
						fail: function(err) {}
					}
				});
			}
		}
	}
</script>

css部分:

<style scoped lang="scss">
	.container {
		padding: 20rpx 20rpx 180rpx 20rpx;

		.current {
			position: fixed;
			right: 0;
			z-index: 99;
			background-color: rgba(0, 0, 0, 0.5);
			color: white;
			padding: 5rpx 20rpx;
			border-radius: 10rpx;
		}

		.img_model {
			width: 100%;
			height: 1000rpx;
			margin: 10rpx 0;
			border: 1rpx solid black;
			border-radius: 10rpx;
			box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.2);
		}
	}

	.btn {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120rpx;
		background-color: #fff;
		box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.2);

		.btn_txt {
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
			background-color: #fa6e08;
			width: 90%;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			color: white;
			font-size: 30rpx;
			border-radius: 10rpx;
		}
	}
</style>

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的uniapp列表滚动加载的实现: 1. 在template中定义列表和加载更多的按钮: ``` <template> <view> <scroll-view scroll-y="true" @scrolltolower="loadMore"> <view v-for="(item, index) in list" :key="index">{{item}}</view> </scroll-view> <button v-if="showLoadMoreBtn" @click="loadMore">加载更多</button> </view> </template> ``` 2. 在script中定义列表数据和是否显示加载更多按钮的变量: ``` <script> export default { data() { return { list: [], // 列表数据 showLoadMoreBtn: false, // 是否显示加载更多按钮 pageNum: 1, // 当前页码 pageSize: 10, // 每页数量 } }, methods: { // 加载更多 loadMore() { // 显示加载更多按钮 this.showLoadMoreBtn = true; // 发送请求获取数据 this.getData().then(res => { // 添加数据到列表 this.list = this.list.concat(res.data); // 隐藏加载更多按钮 this.showLoadMoreBtn = false; // 页码加1 this.pageNum++; }); }, // 发送请求获取数据 getData() { return uni.request({ url: 'your_api_url', data: { pageNum: this.pageNum, pageSize: this.pageSize, }, }); }, }, } </script> ``` 3. 在样式中设置scroll-view的高度,使其可以滚动: ``` <style> scroll-view { height: 500rpx; } </style> ``` 以上就是一个简单的uniapp列表滚动加载的实现。其中,loadMore方法会在滚动到页面底部或点击加载更多按钮时触发,发送请求获取数据并将数据添加到列表中。可以根据实际需求来调整pageSize等参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值