微信小程序下拉刷新上拉加载更多

本文使用的是page页面事件处理函数 onPullDownRefresh() 下拉刷新 和 onReachBottom()上拉触底事件
进行数据上拉刷新和下拉加载。

demo3.json

{
        "navigationBarTitleText": "demo3",
        "navigationBarTextStyle": "white",
        "enablePullDownRefresh": true
}

demo3.js

// pages/demo3/demo3.js
var url = 'http://v.juhe.cn/toutiao/index?type=top&key=3dc86b09a2ee2477a5baa80ee70fcdf'
Page({

  /**
   * 页面的初始数据
   */
  data: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: url, //仅为示例,并非真实的接口地址
      data: {
        type: 'top',
        key: '3dc86b09a2ee2477a5baa80ee70fcdf5'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          news: res.data.result.data,
        });
        console.log(res.data.result.data);
      },
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //显示刷新图标
    wx.showNavigationBarLoading();
    var that = this
    wx.request({
      url: url, //仅为示例,并非真实的接口地址
      data: {
        type: 'top',
        key: '3dc86b09a2ee2477a5baa80ee70fcdf5'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          news: res.data.result.data,
        });
        console.log(res.data);
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      },
    })

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    var that = this;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    
    //隐藏加载图标
    setTimeout(function () {
    wx.request({
      url: url, //仅为示例,并非真实的接口地址
      data: {
        type: 'top',
        key: '3dc86b09a2ee2477a5baa80ee70fcdf5'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      
      success: function (res) {
        // 回调函数
        console.log(1111333);
        var newsMore = []
        //新的数据拼接旧的的数据
        newsMore = that.data.news.concat(res.data.result.data);
       // const oldData = that.data.news;
        that.setData({
          news: newsMore
        });
      }
    })
      //隐藏加载图标
      wx.hideLoading()
    }, 1000)

  },

})

demo3.wxml

<!--pages/demo3/demo3.wxml-->
<view class="">
  <block wx:for="{{news}}">
    <view class="content" catchtap="goNewsTap" data-title="{{item.title}}" data-url = "{{item.url}}">
    <image class="image" src="{{item.thumbnail_pic_s}}"></image>
    <image class="image" src="{{item.thumbnail_pic_s02}}"></image>
    <view class="xw" >
      <text class="title">{{item.title}}\n</text>
      <text class="author_name">{{item.author_name}}\n</text>
      <text class="date">{{item.date}}\n</text>
      <!-- <navigator url="{{item.url}}">详情</navigator> -->
    </view>
  </view>
  </block>  
</view>

demo3.wxss

/* pages/demo3/demo3.wxss */
.content{
 float: left
}
.content .image{
  float: left;
  height: 200rpx;
  width: 250rpx;
  margin-left: 10rpx
}
.xw{
  float: left;
  margin-left: 10rpx
}
.xw .title{
 font-weight: bolder;
}
.xw .date{
 margin-right: 50rpx
}

效果图:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长夜将尽 来日可期

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值