微信小程序-scroll-view

scroll-view.wxml 文件

html 复制代码
<scroll-view 
  scroll-y 
  enable-back-to-top
  refresher-enabled
  class="scroll-y"
  lower-threshold="100"
  bindscrolltolower="getMoreHandler" 
  bindrefresherrefresh="refreshHandler"
  refresher-triggered="{{isTriggered}}">
  <view wx:for="{{ numList }}" wx:key="*this">{{ item }}</view>
</scroll-view>
  1. scroll-y : 上下滑动
  2. enable-back-to-top:点击状态栏回到顶部
  3. refresher-enabled:开启下拉刷新
  4. lower-threshold:距底部/右边多远时,触发 scrolltolower 事件
  5. bindscrolltolower:触发上滑加载事件
  6. refresher-triggered:设置当前下拉刷新状态,true 表示下拉刷新已经被触发,false 表示下拉刷新未被触发

scroll-view.wxss 文件

css 复制代码
.scroll-y{
  height: 100vh; 
  background-color: #efefef;
}

view{
  height: 500rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

view:nth-child(even){
  background-color: sienna;
}

view:nth-child(odd){
  background-color: slateblue;
}

scroll-view.js 文件

javascript 复制代码
Page({

  /**
   * 页面的初始数据
   */
  data: {
    numList: [1, 2, 3],
    isTriggered:false
  },

  getMoreHandler() {
    wx.showLoading({
      title: '加载中...',
    })
    setTimeout(() => {
      const n = this.data.numList[this.data.numList.length - 1]
      const newList = [n + 1, n + 2, n + 3]
      this.setData({
        numList: [...this.data.numList, ...newList]
      })
      wx.hideLoading()
    }, 1000);
  },
  refreshHandler() {
    wx.showToast({
      title: '下拉刷新',
    })
    this.setData({
      numList: [1, 2, 3],
      isTriggered:false
    })
  }
})

注意:

  1. 需要为scroll-view设置高度,否则不能滑动
  2. 如果在手机上预览出现不能上滑并且底部有空白,需要合理调整item和lower-threshold的高度
相关推荐
2501_915918412 小时前
Web 前端可视化开发工具对比 低代码平台、可视化搭建工具、前端可视化编辑器与在线可视化开发环境的实战分析
前端·低代码·ios·小程序·uni-app·编辑器·iphone
2501_915106323 小时前
iOS 使用记录和能耗监控实战,如何查看电池电量消耗、App 使用时长与性能数据(uni-app 开发调试必备指南)
android·ios·小程序·uni-app·cocoa·iphone·webview
じòぴé南冸じょうげん11 小时前
小程序的project.private.config.json是无依赖文件,那可以删除吗?
前端·小程序·json
2501_9160137412 小时前
HTTPS 抓包难点分析,从端口到工具的实战应对
网络协议·http·ios·小程序·https·uni-app·iphone
2501_9159184114 小时前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张14 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
微三云-轩15 小时前
区块链:重构企业数字化的信任核心与创新动力
人工智能·小程序·区块链·生活·我店
阿隆_趣编程17 小时前
为了方便相亲,我用AI写了一款小程序
前端·javascript·微信小程序
2501_915918411 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
黑马源码库miui520861 天前
JAVA同城打车小程序APP打车顺风车滴滴车跑腿源码微信小程序打车源码
java·微信·微信小程序·小程序·uni-app