1. 程式人生 > >微信小程式 --- 6行程式碼實現頁面返回頂部

微信小程式 --- 6行程式碼實現頁面返回頂部

效果預覽:
這裡寫圖片描述

js部分:

Page({
  data: {
    topNum: 0
  },

  returnTop: function () {
    this.setData({
      topNum: this.data.topNum = 0
    });
  }
}) 

wxml部分:

<scroll-view scroll-y scroll-with-animation='true' scroll-top='{{ topNum }}'>
  <view>1</view>
  <view>1</view>
<view>1</view> <view>1</view> <view>1</view> <view>1</view> <view>1</view> <view>1</view> <view>1</view> <view>1</view> </scroll-view> <view class='top' catchtap='returnTop'>頂部</view
>

wxss部分:

page{
  width: 100%;
  height: 100%;
}

scroll-view{
  height: 100%;
  width: 100%;
}

view{
  width: 100%;
  height: 200px;
  text-align: center;
  line-height: 200px;
  font-weight: 600;
}

view:nth-child(odd){
  background-color: red;
}

.top{
  width: 50px;
  height: 50px;
  position: fixed;
  right
: 10px
; bottom: 10px; z-index: 1; background-color: yellow; color: red; text-align: center; line-height: 50px; }