1. 程式人生 > >微信小程式學習筆記四(持續更新)---征服scroll-view下拉重新整理

微信小程式學習筆記四(持續更新)---征服scroll-view下拉重新整理

貼圖 在這裡插入圖片描述 大概實現這種使用swiper做tab切換,資料頁面下拉重新整理的效果。 官方提供的scroll-view作為容器,如果在scroll-view使用onPullDownRefresh實現下拉重新整理,會存在頁面重新整理卡,並且重新整理會出現在tab之上,使用者體驗極差。 這裡先來一波參考文件: scroll-view微信官方文件 scroll-view下拉元件—來自其他大神自己封裝的元件,強推!實現思路還是scroll-view

以下程式碼為示例,並非圖片效果,圖片效果需要根據自己業務修改

具體實現:

1、下載scroll-view下拉元件將所需要的x-scroll-view元件拷至自己專案對應的component檔案目錄下。 2、頁面json檔案中引用元件:

"usingComponents": {
    "x-scroll-view": "../path/x-scroll-view"
    }

3、.wxml中使用元件:

<x-scroll-view refreshing="{{refreshing}}"   style='height:1000rpx;' nomore="{{nomore}}" bindpulldownrefresh="_onPullDownRefresh" bindscroll="_onScroll">
     <view style='min-height:1100rpx'>
       <block wx:for="{{colors}}"  wx:for-index="index" wx:key="index">
         	<view class="view" style="background: {{item}}">{{index + 1}}. {{item}}</view>
       </block>
       </view>
     </x-scroll-view>

注意:必須在迴圈外面巢狀一層view,將高度設定為超過scroll-view的高度,否則在資料高度未超過scroll-view本身高度時,無法觸發下拉重新整理 4、.js檔案中:

Page({

  data: {
    colors: [],
    scrollLeft: 0,
    currentTab: 0,
    currentIndex: 0,
  },
  // tab切換
switchTab: function(e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  // 點選tab導航切換
  switchNav: function(e) {
    let cur = e.target.dataset.current;
    if (this.data.currentTab == cur) {
      return false
    } else {
      this.setData({
        currentTab: cur
      })
    }
  },
  _randomColor: function () {
    return `rgba(${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${Math.floor(Math.random() * 256)},${(Math.random() * 0.3 + 0.2).toFixed(1)})`;
  },

  _generateColors: function (length) {
    return new Array(length).fill(null).map(() => this._randomColor());
  },

  //下拉重新整理監聽函式
  _onPullDownRefresh: function () {
    setTimeout(() => {
      const colors = this._generateColors(1);
      this.setData({
        colors,
        refreshing: false,
      });
    }, 2000);
  },

  //載入更多監聽函式
  _onLoadmore: function () {
    setTimeout(() => {
      if (this.data.colors.length == 80) {
        this.setData({ nomore: true })
      } else {
        const colors = this._generateColors(1);
        this.setData({ colors: [...this.data.colors, ...colors] });
      }
    }, 1000);
  },

  _onScroll: function (e) {
    console.log(e);
  },

  onLoad: function (options) {
    const colors = this._generateColors(1);
    this.setData({ colors });
  },
})

以上程式碼實現下拉便可實現下拉重新整理,再加上頭部tab

<scroll-view scroll-x class='tabHeader' scroll-left="{{scrollLeft}}">
    <view data-current='0' bindtap='switchNav' class="tab-item {{currentIndex==0?'active':''}}">
      <view class="reset {{currentIndex==0?'reset-active':''}}"></view>tab1
    </view>
    <view class="tab-item {{currentIndex==1?'active':''}}" data-current='1' bindtap='switchNav'>
      <view class="reset  {{currentIndex==1?'reset-active':''}}"></view>tab2
     </view>
    <view class="tab-item {{currentIndex==2?'active':''}}" data-current='2' bindtap='switchNav'>
      <view class="reset {{currentIndex==2?'reset-active':''}}"></view>tab3
     </view>
  </scroll-view>
   <swiper duration="300" current='{{currentTab}}' bindchange='switchTab' style='height:{{winHeight}}rpx; background:#F2F2EF'>
   <swiper-item>
   		<x-scroll-view refreshing="{{refreshing}}"   style='height:1000rpx;' nomore="{{nomore}}" bindpulldownrefresh="_onPullDownRefresh" bindscroll="_onScroll">
      		<view style='min-height:1100rpx'>
        		<block wx:for="{{colors}}"  wx:for-index="index" wx:key="index">
          			<view class="view" style="background: {{item}}">{{index + 1}}. {{item}}</view>
        		</block>
        	</view>
      </x-scroll-view>
    </swiper-item>
     <swiper-item>
   		<x-scroll-view refreshing="{{refreshing}}"   style='height:1000rpx;' nomore="{{nomore}}" bindpulldownrefresh="_onPullDownRefresh" bindscroll="_onScroll">
      		<view style='min-height:1100rpx'>
        		<block wx:for="{{colors}}"  wx:for-index="index" wx:key="index">
          			<view class="view" style="background: {{item}}">{{index + 1}}. {{item}}</view>
        		</block>
        	</view>
      </x-scroll-view>
    </swiper-item>
     <swiper-item>
   		<x-scroll-view refreshing="{{refreshing}}"   style='height:1000rpx;' nomore="{{nomore}}" bindpulldownrefresh="_onPullDownRefresh" bindscroll="_onScroll">
      		<view style='min-height:1100rpx'>
        		<block wx:for="{{colors}}"  wx:for-index="index" wx:key="index">
          			<view class="view" style="background: {{item}}">{{index + 1}}. {{item}}</view>
        		</block>
        	</view>
      </x-scroll-view>
    </swiper-item>
</swiper>
      

加上部分wxss程式碼:

page {
height: 100%;
display: flex;
flex-direction: column;
}

x-scroll-view {
height: calc(100% - 51px);
}
.tabHeader {
height: 90rpx;
width: 100%;
line-height: 89rpx;
font-size: 16rpx;
display: flex;
align-items: center;
z-index: 99;
box-shadow:2px 10px 25px rgba(173,139,1,0.1);
}

okok~