微信小程序開發之scroll-view


本文主要介紹通過scroll-view實現回至頂部,如下效果

一、頁面代碼

頂部的工具欄放一個查找按鈕,滾動區域實現分頁,目的就是為了點擊上一頁\下一頁時,自動回到頂部。

scroll-view必須指定scroll-y屬性和該區域的高度

<view class="page">
  <view class="swiper-tab border-top">
    <view>
      <text class='text-primary'>查找</text>
    </view>
  </view>
  <view class="weui-panel weui-panel_access mt0 no_border">
    <scroll-view scroll-y style="height:{{scrollHeight}}px;" scroll-top="{{scrollTop}}">
      
      <view class="weui-panel__bd">
        <block wx:for="{{listdata}}" wx:key="{{item.Id}}">
          <navigator url="/pages/test/Show?id={{item.Id}}">
            <view class="weui-media-box weui-media-box_text">              
              <view class="weui-media-box__desc">{{item.Desc}}</view>
            </view>
          </navigator>
        </block>
      </view>
      <view class="weui-panel__ft">
        <view class="weui-cell weui-cell_access weui-cell_link">
          <view class="weui-cell__bd col-4 text-center" bindtap="prevPage">上一頁</view>
          <view class="weui-cell__bd col-4 text-center">
            <picker bindchange='changePage' class="picker" value="{{currentPage-1}}" range="{{pageArray}}">
              <view>
                第{{pageArray[currentPage-1]}}頁
              </view>
            </picker>
          </view>
          <view class="weui-cell__bd col-4 text-center" bindtap="nextPage">下一頁</view>
        </view>
      </view>
    </scroll-view>
  </view>
</view>

二、腳本代碼

scrollHeight是內容區域的高度,點擊上一頁\下一頁時,回到頂部

Page({
  data: { 
    limit: 10,
    currentPage: 1,
    total: 0,
    pageArray: [], 
    scrollTop: 0,
    scrollHeight: 0
  },
  onLoad: function (options) { 
    var that =this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({ scrollHeight: res.windowHeight - 42 });
      }
    });  
    this.loadMainData();
  },
  loadMainData: function () {
    this.tapMove();
    //加載數據
  }, 
  onPullDownRefresh: function () {
    this.prevPage();
  },
  setPages: function (count) {
    var pages = new Array();
    for (var i = 0; i < count; i++) {
      pages.push(i + 1);
    }
    this.setData({ pageArray: pages });
  },
  prevPage: function () {
    if (this.data.currentPage > 1) {
      this.setData({ currentPage: this.data.currentPage - 1 });
      this.loadMainData();
    } else {
      msg.showToast({ title: '當前是第一頁', icon: 'info' });
    }
  },
  nextPage: function () {
    if (this.data.currentPage < (this.data.total / this.data.limit)) {
      this.setData({ currentPage: this.data.currentPage + 1 });
      this.tapMove();
      this.loadMainData();
    } else {
      msg.showToast({ title: '當前是最后一頁', icon: 'info' });
    }
  },
  changePage: function (e) {
    var page = parseInt(e.detail.value) + 1;
    this.setData({ currentPage: page });
    this.loadMainData();
  },
  tapMove: function (e) {
    this.setData({
      scrollTop:0
    })
  },
})


最近比較忙,就簡單記錄下

 

歡迎閱讀本系列文章:微信小程序開發教程目錄

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM