WXML:
<!-- 返回顶部悬浮 --> <view class="to_top_view" bindtap="toTop" hidden="{{!showToTop}}"> <image src="/resources/images/carBuying/to_top.png" mode="widthFix"></image> </view>
JS:
data: { showToTop: false }, /** * 方法区 */ // 点击回到顶部 toTop() { wx.pageScrollTo({ scrollTop: 0, duration: 500, }) }, /** * 用户滚动页面时 */ onPageScroll(e) { if (e.scrollTop > 780) { this.setData({ showToTop: true }) } else { this.setData({ showToTop: false }) } }
WXSS:
.to_top_view { display: flex; justify-content: center; align-items: center; position: fixed; right: 3%; bottom: 1%; width: 100rpx; height: 100rpx; z-index: 1; }