Home  >  Article  >  WeChat Applet  >  Detailed explanation of how scroll-view completes list pages

Detailed explanation of how scroll-view completes list pages

Y2J
Y2JOriginal
2018-05-14 09:12:543020browse

This article mainly introduces the relevant information on the example code of the scroll-view component of the WeChat applet to implement the list page. Introduction to the scroll-view component scroll-view is a scrollable view component provided by the WeChat applet. Its main function is to use To do the pull-up loading that is often seen on mobile phones, friends who need it can refer to the introduction of

scroll-view component

scroll-view is provided by WeChat applet The scrollable view component, its main function is to do the pull-up to load and pull-down to refresh the list page that is often seen on mobile phones! Let's take "Shake out a smile" as an example to explain the component. Use !

Import a new page for the app

First we need to import a new page for our mini program and open the app in the project root directory.json This projectConfiguration fileIn the pagesarrayAdd "pages/allJoke/allJoke" and then set the bottom NavigationIn the list item of "tabBar" ("list") Add:

 {
   "text": "列表",
   "pagePath": "pages/allJoke/allJoke",
   "iconPath": "images/note.png",
   "selectedIconPath": "images/noteHL.png"
  },

If you want to know the meaning of the specific configuration, you can refer to the Mini Program Configuration document and I won’t go into details here!

json configuration page

The next step is the configuration page for our new page. Create a new directory such as alljoke under the page directory, then create a new allJoke.json under this directory, and copy the following code to this file. Inside:

{
  "navigationBarTitleText": "笑话集锦",
  "enablePullDownRefresh": true
}

Because we need to use the onPullDownRefresh method provided by the applet when doing pull-down refresh later, enablePullDownRefresh must be turned on in the configuration item. The other option is the top title of the page, which you can set at will or not. !

wxml view page

It’s the turn of the view page. Similarly, create a new alljoke.wxml page in the alljoke directory. wxml is the one created by the mini program The view page document type is written like HTML, so it is not difficult for the front-end to get started. If you need to know more about it, you can read the wxml document. Also copy the following code to alljoke.wxml

<view>
 <view>
  <scroll-view class="scroll" scroll-top="{{scrollTop}}" style="height:580px;" scroll-y="true" bindscroll="scrll"  bindscrolltolower="loadMore">
   <view class="block" wx:for="{{listLi}}" wx:for-item="item">
    <text>{{item.text}}</text>
   </view>  
  </scroll-view>
 </view>
 <view class="top" hidden="{{hidden}}" catchtap="goTop">⇧</view>
</view>

As you can see, we The protagonist scroll-view also makes a grand appearance here! What I brought here is a long list of configurations, let me tell you about the functions of these configurations!

##bindscrollCallback function triggered when scrolling##bindscrolltolower

用到的选项都列出来了,还有一点需要大家特别注意的:

使用竖向滚动条时必须为组件设置一个固定高度就是上面代码style里面设置的高!切记切记!

更多资料请阅读微信小程序scroll-view组件文档

wxss样式

同样在alljoke目录下面新建allJoke.wxss文件,小程序的样式和传统css差不多大家可以根据自己喜好自行设计,这里我简单做了一下样式比较丑大家将就着用吧.(题外话:受不了的自己动手丰衣足食)

.block {
  border: 8px solid #71b471;
  margin: 20rpx 20rpx;
  padding: 10rpx;
  background-color: #fff;
  border-radius: 20rpx;
  text-align: center;
}
.top {
  width: 100rpx;
  height: 100rpx;
  line-height: 100rpx;
  background-color: #fff;
  position: fixed;
  bottom: 40rpx;
  right: 20rpx;
  text-align: center;
  font-size: 50rpx;
  opacity: .8;
  border-radius: 50%;
  border: 1px solid #fff; 
}

小程序文档中关于样式的介绍

逻辑部分

来到最后也是最重要的逻辑部分了!老规矩alljoke目录下新建allJoke.js文件,先贴代码再慢慢讲解:

Page({
 data:{
  listLi:[],
  page:1,
  scrollTop:0,
  done: false,
  hidden: true
 },
 onLoad:function(options){
  this.getList(1);
 },

 onPullDownRefresh: function(){
  wx.showToast({
   title: &#39;加载中&#39;,
   icon: &#39;loading&#39;
  });
  this.getList(1,true);
 },

 getList: function(page, stopPull){
  var that = this
  wx.request({
   url: &#39;https://wechat.sparklog.com/jokes&#39;,
   data: {
    page: page,
    per: &#39;20&#39;
   },
   method: &#39;GET&#39;, 
   success: function(res){
    if(page===1){
     that.setData({
      page: page+1,
      listLi: res.data,
      done: false
     })
     if(stopPull){
      wx.stopPullDownRefresh()      
     }
    }else{
     if(res.data<20){
      that.setData({
       page: page+1,
       listLi: that.data.listLi.concat(res.data),
       done: true
      }) 
     }else{
      that.setData({
       page: page+1,
       listLi: that.data.listLi.concat(res.data)
      }) 
     }  
    }
   },
  })
 },

 loadMore: function(){
  var done = this.data.done;
  if(done){
   return
  }else{
   wx.showToast({
    title: &#39;加载中&#39;,
    icon: &#39;loading&#39;,
    duration: 500
   });
   var page = this.data.page;
   this.getList(page)
  }
 },

 scrll: function(e){
  var scrollTop = e.detail.scrollTop
  if(scrollTop>600){
   this.setData({
    scrollTop: 1,
    hidden: false    
   })
  }else{
   this.setData({
    scrollTop: 1,
    hidden: true  
   });
  }
 },

 goTop: function(){
  this.setData({
   scrollTop:0,
   hidden: true 
  })
 }
})

大家可以看到首先我们需要用page()函数注册页面,然后在data里面定义一些初始化数据.onLoad是这个页面的生命周期函数,页面加载时会调用到它.我们在页面加载时调用了自定义的getList函数.这个函数接收两个参数,第一个参数是要加载的页面,第二个参数是布尔值,用来判断是下拉刷新调用的这个函数,还是页面加载时调用的这个函数!接下来onPullDownRefresh是小程序提供的下拉刷新函数,里面wx.showToast显示消息提示框,用来提示用户正在加载,loadMore是滚动到底部触发的事件.函数里面会检查是否已经加载了全部列表项,如果已经加载了全部列表项会return掉,如果数据库还有列表项,上拉到底部加载下一页!scrll函数是滚动时触发的函数,可以看到这个函数会判断滚动条位置是否大于六百,如果大于六百显示点击直达底部的按钮,如果小于六百隐藏直达顶部的按钮.同时会更新滚动条位置的参数.刚刚在讲wxml时已经讲过scroll-view组件设置竖向滚动条位置scroll-top设置项时如果参数一样,页面不会重新渲染,我们就是利用了这一点,如果要到达顶部,位置必定是'0',滚动时触发scrll函数,我们把位置信息设置为'1',因为滚动函数会反复触发,所以此时页面是不会渲染的.也就是说由于位置设置参数都是设置为'1'不变,导致scroll-top设置项不会生效为goTop函数的直达顶部(把参数变为'0'提供机会).最后就是直达顶部按钮的函数了,可以看到它是把位置信息变为'0',参数变化scroll-top设置生效,页面直达顶部.最后再通过改变hidden这个参数把自己(直达顶部按钮)给隐藏掉了!

结尾

ok,通过上面的步骤我们终于实现了下拉刷新上拉加载的列表页功能了,从上面我们可以看到微信提供的接口api还是挺全面的,要实现一个功能总体来说要比原生js实现要简单一些!

【相关推荐】

1. 特别推荐“php程序员工具箱”V0.1版本下载

2. 微信公众号平台源码下载

3. 阿狸子订单系统源码下载

Configuration item Function
scroll-top Set the position of the vertical scroll bar. Please pay attention to if it is set The value does not change and the component will not render!
scroll-y Allow vertical scrolling
Event triggered when scrolling to the bottom

The above is the detailed content of Detailed explanation of how scroll-view completes list pages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn