Wechat Learning Notes 4 (Continuous Updates) - - Conquer scroll-view drop-down refresh

Keywords: Web Development github JSON

Map

Probably to achieve this use of swiper tab switching, data page drop-down refresh effect.
Officially provided scroll-view serves as a container. If scroll-view uses onPull Down Refresh to achieve drop-down refresh, there will be a page refresh card, and refresh will appear on tab, and the user experience is very poor.
Here's a list of reference documents:
scroll-view official document for wechat
scroll-view drop-down component From other gods encapsulated components, push! Realization or scroll-view

The following code is an example, not a picture effect, the picture effect needs to be modified according to their own business.

Specific realization:

1, Download scroll-view drop-down component Copy the required x-scroll-view component to the component file directory corresponding to your project.
2. Reference component in page json file:

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

3. wxml uses components:

<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>

Note: A layer of view must be nested outside the loop to set the height above scroll-view, otherwise the drop-down refresh cannot be triggered when the data height does not exceed scroll-view itself.
4. js file:

Page({

  data: {
    colors: [],
    scrollLeft: 0,
    currentTab: 0,
    currentIndex: 0,
  },
  // tab switching
switchTab: function(e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  // Click tab navigation switch
  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());
  },

  //Dropdown refresh listener function
  _onPullDownRefresh: function () {
    setTimeout(() => {
      const colors = this._generateColors(1);
      this.setData({
        colors,
        refreshing: false,
      });
    }, 2000);
  },

  //Load more listening functions
  _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 });
  },
})

Pull-down refresh can be achieved by the above code, plus head 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>
      

Add some wxss code:

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~

Posted by Diggler on Fri, 25 Jan 2019 01:15:15 -0800