Home > Article > WeChat Applet > How to implement WeChat applet click to return to the top level
This article mainly introduces in detail the method of clicking to return to the top level of the WeChat applet. It has certain reference value. Interested friends can refer to it.
I have been studying WeChat applet recently. I was fooled by this return to the top level. Here is the code
wxml code:
<scroll-view scroll-y style="height: 1000rpx;" scroll-top="50" enable-back-to-top="true" scroll-top="{{scrollTop.scroll_top}}" bindscroll="scrollTopFun"> <block wx:for="{{sortArr}}"> <template is="spL" data="{{item}}"></template> </block> </scroll-view> <!-- 联系客服 --> <view class="findOur fliexBox"><contact-button type="default-light" size="15" session-from="weapp"></contact-button>客服</view> <!-- 拨打电话 --> <view class="callOur fliexBox" bindtap="call">电话</view> <view class="fliexBox" style=" bottom: 150rpx; border: solid 1px green;" wx:if="{{scrollTop.goTop_show}}" catchtap="goTopFun">顶层</view>
js code:
var app = getApp(); Page({ data: { hidden: true, list: [], scrollTop: { scroll_top: 0, goTop_show: false }, scrollHeight: 0, floorstatus:true, sortArr:[ { id: 1, img: "../../images/2.jpg", title: "君御豪园住宅", introduction: "杭州不限购不限贷口住宅", money: 5000, vperson: 115, tperson: 0 } ], }, scrollTopFun: function (e) { console.log(e.detail); if (e.detail.scrollTop > 300) {//触发gotop的显示条件 this.setData({ 'scrollTop.goTop_show': true }); } else { this.setData({ 'scrollTop.goTop_show': false }); } }, goTopFun: function (e) { var _top = this.data.scrollTop.scroll_top;//发现设置scroll-top值不能和上一次的值一样,否则无效,所以这里加了个判断 if (_top == 0) { _top = 1; } else { _top = 0; } this.setData({ 'scrollTop.scroll_top': _top }); }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var that = this; wx.getSystemInfo({ success: function (res) { that.setData({ scrollHeight: res.windowHeight }); } }); }, })
wxss code:
.fliexBox{ width: 100rpx; height: 50rpx; background-color: #5db13b; color: #ffffff; text-align: center; position: fixed; right: 0rpx; bottom: 85rpx; border-radius: 20rpx 0rpx 0rpx 20rpx; font-size: 26rpx; line-height: 50rpx; opacity: .6; } .callOur{ bottom: 20rpx; } contact-button{ opacity: 0; position: absolute; }
Main It is necessary to set the height of the scroll-view component and it cannot be a percentage such as 100%. It can be rpx, so that the sliding distance can be monitored.
The above is the detailed content of How to implement WeChat applet click to return to the top level. For more information, please follow other related articles on the PHP Chinese website!