阅读 记录当前位置

uni-app阅读 记录当前位置

核心代码在监听页面滚动

<script>

		onLoad(options) {
			this.id = options.id
			this.getSystenInfo()
			this.getDetails()
		},
		// 监听页面触底
		onReachBottom(e){
			this.reachBottom = 1
		},
		// 监听滚动
		onPageScroll(e) {
			// 计算思路:当前阅读位置 = (设备高度 + 滚动条距离顶部高度) / 当前页面总高度
			this.scrollTop = e.scrollTop
			let sum = (this.screenHeight + this.scrollTop) / this.height
			// 存取每次获取的位置
			this.arr.push(sum.toFixed(2))
			// 取数组中最大值 返回给后端, 这里必须用扩展运算符! Infinity 为 可能没获取到当前页面的高度 需要重新获取
			if(Math.max(...this.arr) == Infinity){
				this.$u.getRect('.pa').then(val => {
					this.height = val.height
				})
			}else{
				this.value = Math.max(...this.arr)
			}
		},
		// 监听页面卸载
		onUnload() {
			let value = 0
			if(this.reachBottom == 1){
				value = 1
			}else{
				value = this.value
			}
			this.$u.post('api/college/addProgress',{
				id:this.id,
				value:value.toString()
			}).then(res=>{
				
			})
		},
		methods:{
			// 获取详情
			getDetails(){
				uni.showLoading({
					title: '正在加载'
				});
				let that = this
				this.$u.get('api/college/getDetails',{
					id:this.id
				}).then(res => {
					if(res.code == 1){
						this.details = res.data
						setTimeout(function(){
							that.$nextTick(() => {
								that.$u.getRect('.pa').then(val => {
									console.log(val.height);
									that.height = val.height
									uni.hideToast();
								})
							})
						},1000)
					}
				})
			},
			// 获取屏幕高度
			getSystenInfo(){
				uni.getSystemInfo({
				    success: res => {
						this.screenHeight = res.screenHeight
				    }
				});
			},
			onNavigateTo(url){
				uni.navigateTo({
					url:url
				})
			}
		},
	
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值