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>