微信小程序回到顶部,点击滚动到顶部简单易懂!

发布于 5 年前作者 yegang4726 次浏览最后编辑 5 年前来自 share

使用scroll-view的情况

// HTML
<view bindtap="toTop">回到顶部</view>
<scroll-view class="scroll_view" scroll-top="{{topNum}}" scroll-y >
内容
</scroll-view>

// js
page({
	data: {
		topNum : 0
	},
	toTop() { // 回到顶部
        this.setData({
            topNum:0
        })
    },
})

未使用scroll-view的情况

// html
<view bindtap="toTop">回到顶部</view>
<view>内容</view>
//js
page({
	toTop(){
		wx.pageScrollTo({
		    scrollTop: 0
		})
	},
})
1 回复
chengguiying
chengguiying1 楼3 个月前

那到底要不要用scroll-view