uniapp点击按钮页面滚动到自定义位置

217 阅读1分钟

功能介绍:记录存在很多条的情况下,需要通过点击一个按钮,跳转到该功能的最开始的地方,由于上面的功能高度不一致,所以需要动态去获取滚动的高度。 在这里插入图片描述

<view class="bottomMenu history u-rela" @click="viewHistory">
	<u-badge type="error" :count="historyNum" :offset="[-10, -10]"></u-badge>
</view>
//js部分
data() {
    return {
        scrollTop: 0,
        statusHeight:0
    }
},
onPageScroll(e) {
    //获取滚动高度
	this.scrollTop = e.scrollTop;
},
onLoad(args) {
	uni.getSystemInfo({
       	success:(res)=>{
		//获取状态栏高度 
		this.statusHeight = res.statusBarHeight;
       }
    });
},
methods: {
    //点击按钮跳转
	viewHistory() {
    	var that = this;
   		this.$u.getRect('.jumpHistory').then(res => {
            uni.pageScrollTo({
                //移动高度=滚动高度 + 距离上面的位置 - 导航条 - 状态栏高度
                scrollTop: that.scrollTop + res.top -44 - that.statusHeight,
                duration: 300
            });
        })
    },
}