小程序----页面事件(二)

简介: 小程序----页面事件(二)

2.3. 配置上拉触底距离

上拉触底距离指的是触发上拉触底事件时,滚动条距离页面底部的距离。

可以在全局或页面的 .json 配置文件中,通过 onReachBottomDistance 属性来配置上拉触底的距离。小程序默认的触底距离是 50px,在实际开发中,可以根据自己的需求修改这个默认值。

3. 上拉触底案例

3.1. 案例效果展示

3.2. 案例的实现步骤

  1. 定义获取随机颜色的方法
  2. 在页面加载时获取初始数据
  3. 渲染 UI 结构并美化页面效果
  4. 在上拉触底时调用获取随机颜色的方法
  5. 添加 loading 提示效果
  6. 对上拉触底进行节流处理

3.3.

步骤1 - 定义获取随机颜色的方法

// pages/home/home.js
Page({
  data: {
    colorList: []
  },
  getColor() {
    wx.request({
      url: 'https://www.escook.cn/api/color',
      method: 'GET',
      success: ({ data: res })=>{
        this.setData( {
          colorList: [...this.data.colorList, ...res.data]
        } )
      }
    })
  }
})

步骤2 - 在页面加载时获取初始数据

// pages/home/home.js
Page({
  data: {
    colorList: []
  },
  getColor() {
    wx.request({
      url: 'https://www.escook.cn/api/color',
      method: 'GET',
      success: ({ data: res })=>{
        this.setData( {
          colorList: [...this.data.colorList, ...res.data]
        } )
      }
    })
  },
  onLoad() {
    this.getColor()
  }
})

步骤3 - 渲染 UI 结构并美化页面效果

<!--pages/home/home.wxml-->
<text>pages/home/home.wxml</text>
<view
  wx:for="{{colorList}}"
  wx:key="index"
  class="num-item"
  style="background-color: rgba({{item}});"
>
  {{item}}
</view>
/* pages/home/home.wxss */
.num-item {
  border: solid 1px black;
  height: 50px;
  margin: 5px;
}

步骤4 - 上拉触底时获取随机颜色

// pages/home/home.js
Page({
  data: {
    colorList: []
  },
  getColor() {
    wx.request({
      url: 'https://www.escook.cn/api/color',
      method: 'GET',
      success: ({ data: res })=>{
        this.setData( {
          colorList: [...this.data.colorList, ...res.data]
        } )
      }
    })
  },
  onLoad() {
    this.getColor()
  },
  // 下拉触底
  onReachBottom() {
    this.getColor()
  }
})

步骤5 - 添加 loading 提示效果

// pages/home/home.js
Page({
  data: {
    colorList: []
  },
  getColor() {
    // loading
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: 'https://www.escook.cn/api/color',
      method: 'GET',
      success: ({ data: res })=>{
        this.setData( {
          colorList: [...this.data.colorList, ...res.data]
        } )
      },
      // 请求完成
      complete: ()=>{
        wx.hideLoading()
      }
    })
  },
  onLoad() {
    this.getColor()
  },
  onReachBottom() {
    this.getColor()
  }
})

步骤6 - 对上拉触底进行节流处理

  1. 在 data 中定义 isloading 节流阀
    false 表示当前没有进行任何数据请求
    true 表示当前正在进行数据请求
  2. 在 getColors() 方法中修改 isloading 节流阀的值
    在刚调用 getColors 时将节流阀设置 true
    在网络请求的 complete 回调函数中,将节流阀重置为 false
  3. 在 onReachBottom 中判断节流阀的值,从而对数据请求进行节流控制
    如果节流阀的值为 true,则阻止当前请求
    如果节流阀的值为 false,则发起数据请求
// pages/home/home.js
Page({
  data: {
    colorList: [],
    // 节流
    isLoading: false
  },
  getColor() {
    // loading
    // 节流
    if ( this.data.isLoading ) return 
    // 节流
    this.setData( {
      isLoading: true
    } )
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: 'https://www.escook.cn/api/color',
      method: 'GET',
      success: ({ data: res })=>{
        this.setData( {
          colorList: [...this.data.colorList, ...res.data]
        } )
      },
      // 请求完成
      complete: ()=>{
        wx.hideLoading()
        // 节流
        this.setData( {
          isLoading: false
        } )
      }
    })
  },
  onLoad() {
    this.getColor()
  },
  onReachBottom() {
    this.getColor()
  }
})

拓展


相关文章
|
1月前
|
缓存 小程序 UED
微信小程序如何在切换页面后原页面状态不变
微信小程序如何在切换页面后原页面状态不变
29 0
|
1月前
|
小程序 UED 开发者
小程序如何监听页面的滚动事件
小程序如何监听页面的滚动事件
22 0
|
2月前
|
JSON 小程序 JavaScript
【微信小程序】-- 自定义组件 - 组件所在页面的生命周期 & 插槽(三十七)
【微信小程序】-- 自定义组件 - 组件所在页面的生命周期 & 插槽(三十七)
|
1月前
|
小程序
小程序页面路由传参的方法?
小程序页面路由传参的方法?
14 0
|
1月前
|
缓存 小程序 开发者
微信小程序如何刷新当前页面
微信小程序如何刷新当前页面
17 0
|
2月前
|
存储 JSON 小程序
【微信小程序】-- 页面处理总结(三十一)
【微信小程序】-- 页面处理总结(三十一)
|
2月前
|
JSON 小程序 API
【微信小程序】-- 案例 - 本地生活(列表页面)(三十)
【微信小程序】-- 案例 - 本地生活(列表页面)(三十)
|
2月前
|
小程序
【微信小程序】-- 页面事件 - 上拉触底 - 案例(二十七)
【微信小程序】-- 页面事件 - 上拉触底 - 案例(二十七)
|
2月前
|
JSON 小程序 JavaScript
【微信小程序】-- 页面事件 - 上拉触底(二十六)
【微信小程序】-- 页面事件 - 上拉触底(二十六)
|
11天前
|
存储 编解码 小程序
抖音小程序开发中遇见的坑点
在抖音小程序开发中,需注意10大坑点:遵守小程序限制与规范;解决兼容性问题;优化数据加载速度;适应分享功能限制;处理视频播放挑战;优化图片加载显示;管理资源文件;提升用户体验;考虑安全性;及时更新维护。通过测试、优化和遵循官方文档,可克服这些问题,打造优质小程序。