轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

微信小程序下拉刷新,上拉加载及下拉列表制作

发布者: mitsutoki | 发布时间: 2019-1-27 13:12| 查看数: 5708| 评论数: 1|帖子模式

一:下拉刷新 上拉加载

下拉刷新

小程序页面集成了下拉功能,并提供了接口,我们只需要一些配置就可以拿到事件的回调。

  1. 需要在 .json 文件中配置。 如果配置在app.json文件中,那么整个程序都可以下拉刷新。如果写在具体页面的.json文件中,那么就是对应的页面,可以下拉刷新。

具体页面的.json文件:

  1. {
  2. "enablePullDownRefresh": true
  3. }

app.json文件:

  1. "window": {
  2. "enablePullDownRefresh": true
  3. }
  1. 在js文件中添加回调函数
  1. // 下拉刷新回调接口
  2. onPullDownRefresh: function () {
  3. // do somthing
  4. },
  1. 添加数据

通常情况下的下拉刷新操作,就是把查询条件重置,让页面显示最新的一页数据。下面是笔者demo中的下拉刷新回调接口的代码,同时也是一般情况下的操作流程。

  1. // 下拉刷新回调接口
  2. onPullDownRefresh: function () {
  3. // 我们用total和count来控制分页,total代表已请求数据的总数,count代表每次请求的个数。
  4. // 刷新时需把total重置为0,代表重新从第一条请求。
  5. total = 0;
  6. // this.data.dataArray 是页面中绑定的数据源
  7. this.data.dataArray = [];
  8. // 网络请求,重新请求一遍数据
  9. this.periphery();
  10. // 小程序提供的api,通知页面停止下拉刷新效果
  11. wx.stopPullDownRefresh;
  12. },

上拉加载

同下拉刷新一样,小程序中也提供了用于上拉时回调的接口。官方文档中并没有很详细的介绍,经测试发现,上拉回调的接口并不需要额外的配置(下拉时需要在 .json文件中配置 "enablePullDownRefresh": true),直接在页面滑动到底部时就能拿到回调。

  1. 在js文件中添加回调函数
  1. // 上拉加载回调接口
  2. onReachBottom: function () {
  3. // 我们用total和count来控制分页,total代表已请求数据的总数,count代表每次请求的个数。
  4. // 上拉时需把total在原来的基础上加上count,代表从count条后的数据开始请求。
  5. total += count;
  6. // 网络请求
  7. this.periphery();
  8. },

二:制作下拉列表

 
wxml代码:

  1. <view class="phone_one" bindtap="clickPerson">
  2. <view class="phone_personal">{{firstPerson}}</view>
  3. <image src="../../image/v6.png" class="personal_image {{selectArea ? 'rotateRight' :''}}"></image> //三目法判断图片要不要旋转180。
  4. </view>
  5. <view class="person_box">
  6. <view class="phone_select" hidden="{{selectPerson}}">
  7. <view class="select_one" bindtap="mySelect" data-me="你好">你好</view>
  8. <view class="select_one" bindtap="mySelect" data-me="他好">他好</view>
  9. <view class="select_one" bindtap="mySelect" data-me="大家好">大家好</view>
  10. </view>
  11. </view>

wxss代码:

  1. ```
  2. .phone_personal{
  3. width: 100%;
  4. color:rgb(34, 154, 181);
  5. height:100rpx;
  6. line-height:100rpx;
  7. text-align: center;
  8. }
  9. .phone_one{
  10. display: flex; //用flex布局更方便。
  11. position: relative;
  12. justify-content: space-between;
  13. background-color:rgb(239, 239, 239);
  14. width:90%;
  15. height:100rpx;
  16. margin:0 auto;
  17. border-radius: 10rpx;
  18. border-bottom:2rpx solid rgb(255, 255, 255);
  19. }
  20. .person_box{
  21. position: relative;
  22. }
  23. .phone_select{
  24. margin-top:0;
  25. z-index: 100;
  26. position: absolute; //小程序中z-index和absolute需要同时存在,元素才能脱离文档。
  27. }
  28. .select_one{
  29. text-align: center;
  30. background-color:rgb(239, 239, 239);
  31. width:676rpx; //脱离文档后元素width不能再用百分比。
  32. height:100rpx;
  33. line-height:100rpx;
  34. margin:0 5%;
  35. border-bottom:2rpx solid rgb(255, 255, 255);
  36. }
  37. .personal_image{
  38. z-index: 100;
  39. position: absolute;
  40. right:2.5%;
  41. width: 34rpx;
  42. height: 20rpx;
  43. margin:40rpx 20rpx 40rpx 0;
  44. transition: All 0.4s ease;
  45. -webkit-transition: All 0.4s ease;
  46. }
  47. .rotateRight{
  48. transform: rotate(180deg); //180°旋转图片。
  49. }

js代码:

  1. Page({
  2. data:{
  3. selectPerson:true,
  4. firstPerson:'个人',
  5. selectArea:false,
  6. },
  7. //点击选择类型
  8. clickPerson:function(){
  9. var selectPerson = this.data.selectPerson;
  10. if(selectPerson == true){
  11. this.setData({
  12. selectArea:true,
  13. selectPerson:false,
  14. })
  15. }else{
  16. this.setData({
  17. selectArea:false,
  18. selectPerson:true,
  19. })
  20. }
  21. } ,
  22. //点击切换
  23. mySelect:function(e){
  24. this.setData({
  25. firstPerson:e.target.dataset.me,
  26. selectPerson:true,
  27. selectArea:false,
  28. })
  29. },
  30. onLoad:function(options){
  31. // 页面初始化 options为页面跳转所带来的参数
  32. },
  33. onReady:function(){
  34. // 页面渲染完成
  35. },
  36. onShow:function(){
  37. // 页面显示
  38. },
  39. onHide:function(){
  40. // 页面隐藏
  41. },
  42. onUnload:function(){
  43. // 页面关闭
  44. }
  45. })

最新评论

zuojurong 发表于 2022-7-17 11:18
网站制作怎么看

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表