小程序API —— 57 拓展 - 增强 scroll-view

目录

  • [1. 配置基本信息](#1. 配置基本信息)
  • [2. 实现上拉加载更多功能](#2. 实现上拉加载更多功能)
  • [3. 实现快速回到顶部功能](#3. 实现快速回到顶部功能)
  • [4. 实现下拉刷新功能](#4. 实现下拉刷新功能)

scroll-view 组件功能非常强大,这里使用 scroll-view 实现上拉加载和下拉刷新功能;

下面使用微信开发者工具来演示一下具体用法:

1. 配置基本信息

  • 在 pages/cate/cae.wxml 中添加页面框架:

    html 复制代码
    <scroll-view scroll-y class="scroll-y">
      <view wx:for="{{ numList }}" wx:key="*this">{{ item }}</view>
    </scroll-view>
  • 在 pages/cate/cate.scss 中添加页面样式:

    css 复制代码
    .scroll-y {
      height: 100vh;
      background-color: #efefef;
    }
    
    view {
      height: 400rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    view:nth-child(odd){
      background-color: skyblue;
    }
    
    view:nth-child(even){
      background-color: lightsalmon;
    }
  • 在 pages/cate/cate.js 中添加页面使用的数据:

    javascript 复制代码
    Page({
      data: {
        numList: [1, 2, 3]
      }
    })
  • 基本页面如下所示:

2. 实现上拉加载更多功能

  • 修改 pages/cate/cate.wxml 文件,在 scroll-view 中添加新的参数:

    html 复制代码
    <scroll-view
      scroll-y 
      class="scroll-y"
      lower-threshold="100"
      bindscrolltolower="getMore"
    >
      <view wx:for="{{ numList }}" wx:key="*this">{{ item }}</view>
    </scroll-view>
  • 修改 pages/cate/cate.js 文件,添加 getMore 方法,用于下拉刷新:

    javascript 复制代码
    Page({
      data: {
        numList: [1, 2, 3]
      },
    
      // scroll-view 上拉加载更多事件的事件处理函数
      getMore(){
        wx.showLoading({
          title: '数据加载中...',
        })
    
        setTimeout(()=>{
          // 获取数组的最后一项
          const lastNum = this.data.numList[this.data.numList.length - 1]
    
          // 定义需要追加的元素
          const newArr = [lastNum + 1, lastNum + 2, lastNum + 3]
    
          this.setData({
            numList:[...this.data.numList, ...newArr]
          })
    
          wx.hideLoading()
        }, 1500)
      }
    })
  • 点击编译,可以看到使用 scroll-view 实现了下拉功能:

3. 实现快速回到顶部功能

如果我们下拉访问的数据比较多,如果想返回第一条数据,需要一直向上滑动,比较麻烦;在微信中有一个功能,如果是IOS可以点击顶部状态栏,如果是安卓可以点击标题栏,这两种方式可以使滚动条返回顶部,下面我们演示这个属性:

  • 在 pages/cate/cate.wxml 中的 scroll-view 中添加 enable-back-to-top 属性:

  • 点击顶部的编译,用手机微信扫描,在手机端下拉刷新,然后双击顶部的状态栏或者标题栏,就可以返回第一条数据位置:

4. 实现下拉刷新功能

  • 在 pages/cate/cate.wxml 中配置下拉刷新功能:

    html 复制代码
    <scroll-view
      scroll-y 
      class="scroll-y"
      lower-threshold="100"
      bindscrolltolower="getMore"
      enable-back-to-top
    
      refresher-enabled 
      refresher-default-style="black"
      refresher-background="f7f7f8"
      bindrefresherrefresh="refreshHandler"
      refresher-triggered="{{isTriggered}}"
    >
      <view wx:for="{{ numList }}" wx:key="*this">{{ item }}</view>
    </scroll-view>
  • 在 pages/cate/cate.js 中配置下拉刷新方法:

    javascript 复制代码
    Page({
      data: {
        numList: [1, 2, 3],
        isTriggered: false  // 用于下拉刷新 loading 页面收回
      },
    
      refreshHandler () {
        wx.showToast({
          title: '下拉刷新...',
        })
    
        // 当用户上拉加载更多以后,如果用户进行了下拉刷新,需要将数据进行重置
        this.setData({
          numList: [1, 2, 3],
          isTriggered: false
        })
      },
    
      // scroll-view 上拉加载更多事件的事件处理函数
      getMore(){
        wx.showLoading({
          title: '数据加载中...',
        })
    
        setTimeout(()=>{
          // 获取数组的最后一项
          const lastNum = this.data.numList[this.data.numList.length - 1]
    
          // 定义需要追加的元素
          const newArr = [lastNum + 1, lastNum + 2, lastNum + 3]
    
          this.setData({
            numList:[...this.data.numList, ...newArr]
          })
    
          wx.hideLoading()
        }, 1500)
      }
    })
  • 点击编译,下拉刷新之后,可以看到,数据恢复到 3 个厨师数据,如下:

参考视频:尚硅谷微信小程序开发教程

相关推荐
说私域5 小时前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序
fakaifa13 小时前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_9159184120 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售1 天前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
suncentwl1 天前
做一个答题pk小程序多少钱?
小程序·答题小程序·知识竞赛·答题pk软件
说私域1 天前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化策略研究
人工智能·小程序
咸虾米_1 天前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
微信小程序·小程序·uniapp开发·小程序地图api
未来之窗软件服务1 天前
蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
小程序·自动化·仙盟创梦ide·东方仙盟·蔬菜批发·批发系统
数据皮皮侠1 天前
最新上市公司业绩说明会文本数据(2017.02-2025.08)
大数据·数据库·人工智能·笔记·物联网·小程序·区块链
不如摸鱼去2 天前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程