详解微信小程序开发之下拉刷新 上拉加载

所属分类: 网络编程 / JavaScript 阅读数: 1104
收藏 0 赞 0 分享

微信小程序中的下拉刷新,上拉加载的功能很常见,目前我知道的有两种可行的方法,一是scroll-view,二是整个页面刷新.今天说说第一种,自己造轮子,难免有些瑕疵,日后慢慢完善.

上gif:
原理: scroll-view中有监听滑动的方法,这个跟Android类似.其中用到了滑动到顶部,滑动到底部的方法.

1.下拉刷新,在滑动到顶部时,bindscrolltoupper被调用,根据自己的业务逻辑请求即可.我的demo只是随机换了个关键字.

2.上拉加载,在滑动到底部时,bindscrolltolower被调用,我这里是页数加一,根据自己的业务逻辑修改,然后将获取到的集合添加到scroll-view的数据集合里即可.

上代码:

1.index.js

//index.js 
//获取应用实例 
var app = getApp() 
Page({ 
 data: { 
 words: [], 
 windowHeight: 0,//获取屏幕高度 
 refreshHeight: 0,//获取高度 
 refreshing: false,//是否在刷新中 
 refreshAnimation: {}, //加载更多旋转动画数据 
 clientY: 0,//触摸时Y轴坐标 
 }, 
 onLoad: function () { 
 var _this = this; 
 //获取屏幕高度 
 wxgetSystemInfo({ 
  success: function (res) { 
  _thissetData({ 
   windowHeight: reswindowHeight 
  }) 
  consolelog("屏幕高度: " + reswindowHeight) 
  } 
 }) 
 //获取words 
 wxrequest({ 
  url: 'http://apiavatardatacn/ChengYu/Search?key=77f072d28eb141c8b6dda145ca364b92&keyWord=好', 
  complete: function (res) { 
  if (resdatareason == 'Succes') { 
   _thissetData({ 
   words: resdataresult 
   }) 
  } 
  } 
 }) 
 }, 
 scroll: function () { 
 consolelog("滑动了") 
 }, 
 lower: function () { 
 var start = 0; 
 start += 1; 
 consolelog("加载了") 
 var _this = this; 
 wxrequest({ 
  url: 'http://apiavatardatacn/ChengYu/Search', 
  data: { 
  key: '77f072d28eb141c8b6dda145ca364b92', keyWord: '好', page: start 
  }, 
  complete: function (res) { 
  if (resdatareason == 'Succes') { 
   var words = _thisdatawordsconcat(resdataresult); 
   _thissetData({ 
   words: words 
   }) 
  } 
  } 
 }) 
 }, 
 upper: function () { 
 consolelog("下拉了") 
 //获取用户Y轴下拉的位移 
 
 if (thisdatarefreshing) return; 
 thissetData({ refreshing: true }); 
 updateRefreshIconcall(this); 
 var _this = this; 
 var i = Mathrandom() //获得0-1的随机数 
 i = Mathceil(i * 10) //乘以10并向上去整 
 var words = ['龙', '一', '万', '千', '浩', '金', '得', '而', '可', '人']; 
 var word = words[i]; 
 wxrequest({ 
  url: 'http://apiavatardatacn/ChengYu/Search?key=77f072d28eb141c8b6dda145ca364b92&keyWord=' + word, 
 
  complete: function (res) { 
  if (resdatareason == 'Succes') { 
   setTimeout(function () { 
   _thissetData({ 
    words: resdataresult 
   }) 
   }, 2000) 
  } 
  setTimeout(function () { 
   _thissetData({ 
   refreshing: false 
   }) 
  }, 2500) 
  } 
 }) 
 }, 
 start: function (e) { 
 var startPoint = etouches[0] 
 var clientY = startPointclientY; 
 thissetData({ 
  clientY: clientY, 
  refreshHeight: 0 
 }) 
 }, 
 end: function (e) { 
 var endPoint = echangedTouches[0] 
 var y = (endPointclientY - thisdataclientY) * 6; 
 if (y > 50) { 
  y = 50; 
 } 
 thissetData({ 
  refreshHeight: y 
 }) 
 }, 
 move: function (e) { 
 consolelog("下拉滑动了") 
 } 
}) 
 
/** 
 * 旋转上拉加载图标 
 */ 
function updateRefreshIcon() { 
 var deg = 0; 
 var _this = this; 
 consolelog('旋转开始了') 
 var animation = wxcreateAnimation({ 
 duration: 1000 
 }); 
 
 var timer = setInterval(function () { 
 if (!_thisdatarefreshing) 
  clearInterval(timer); 
 animationrotateZ(deg)step();//在Z轴旋转一个deg角度 
 deg += 360; 
 _thissetData({ 
  refreshAnimation: animationexport() 
 }) 
 }, 1000); 
} 

2.index.wxml

<!--indexwxml--> 
 <view class="refresh-block" style="height: {{refreshHeight}}px;" wx:if="{{refreshing}}"> 
 <image animation="{{refreshAnimation}}" src="/images/refreshpng"></image> 
 </view> 
<scroll-view scroll-y="true" style="height: {{windowHeight}}px;" bindscroll="scroll" bindscrolltolower="lower" bindscrolltoupper="upper" 
catchtouchmove="move" catchtouchstart="start" catchtouchend="end" 
> 
<block wx:for="{{words}}"> 
  <view class="item-style">{{itemname}}</view> 
</block> 
</scroll-view> 

3.index.wxss

/**indexwxss**/ 
item-style{ 
 padding: 30rpx; 
 font-size: 40rpx; 
 text-align: center; 
 border-top: 2rpx solid #eee; 
} 
refresh-block { 
 padding: 15px; 
 text-align: center 
} 
refresh-block image { 
 width: 30px; 
 height: 30px; 
} 

demo:下载地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Angular使用Md5加密的解决方法

这篇文章主要介绍了Angular使用Md5加密的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解JS构造函数中this和return

本文通过实例代码给大家介绍了JS构造函数中this和return,需要的朋友参考下吧
收藏 0 赞 0 分享

ES6中Array.find()和findIndex()函数的用法详解

ES6为Array增加了find(),findIndex函数。find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined,而findIndex()函数也是查找目标元素,找到就返回元素的位置,找不到就返回-1。下面通过实例详解,需要的朋友参考下吧
收藏 0 赞 0 分享

JS闭包的几种常见形式实例详解

本文通过实例代码给大家详细介绍了js闭包的几种常见形式,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下
收藏 0 赞 0 分享

ES6中Array.copyWithin()函数的用法实例详解

ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去。下面重点给大家介绍ES6中Array.copyWithin()函数的用法,需要的朋友参考下
收藏 0 赞 0 分享

Javascript 严格模式use strict详解

严格模式:由ECMA-262规范定义的JavaScript标准,对javascrip的限制更强。这篇文章主要介绍了Javascript 严格模式use strict详解 ,需要的朋友可以参考下
收藏 0 赞 0 分享

引入JavaScript时alert弹出框显示中文乱码问题

今天在HTML中引入JavaScript文件运行时,alert弹出的提示框中文显示为乱码,怎么解决此问题呢?下面小编给大家带来了引入JavaScript时alert弹出框显示中文乱码问题的解决方法,一起看看吧
收藏 0 赞 0 分享

AngularJs 延时器、计时器实例代码

这篇文章主要介绍了AngularJs 延时器、计时器实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

JS分页的实现(同步与异步)

这篇文章主要介绍了JS分页的实现(同步与异步),需要的朋友可以参考下
收藏 0 赞 0 分享

Angularjs自定义指令实现分页插件(DEMO)

由于最近的一个项目使用的是angularjs1.0的版本,涉及到分页查询数据的功能,后来自己就用自定义指令实现了该功能,下面小编把实例demo分享到脚本之家平台,需要的朋友参考下
收藏 0 赞 0 分享
查看更多