微信小程序-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的高度
相关推荐
Stanford_11065 小时前
【2026新年启程】学习之路,探索之路,技术之路,成长之路……都与你同行!!!
前端·c++·学习·微信小程序·排序算法·微信开放平台
初学者52136 小时前
微信小程序调用录音没反应,本地开发测试却没问题,operateRecorderfail appid privacy api banned
微信小程序·小程序
微爱帮监所写信寄信6 小时前
微爱帮监狱寄信写信小程序信件内容实时保存技术方案
java·服务器·开发语言·前端·小程序
说私域8 小时前
基于开源AI大模型AI智能名片S2B2C商城小程序的内容价值生成与多点选择传播策略研究
人工智能·微信·小程序·开源
2501_916007471 天前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
iOS 抓包工具有哪些?不同类型的抓包工具可以做什么
android·ios·小程序·https·uni-app·iphone·webview
云起SAAS1 天前
供求求购供应发布VIP会员抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·供求求购供应发布
毕设源码-朱学姐1 天前
【开题答辩全过程】以 基于微信小程序旅游疫情防控管理系统为例,包含答辩的问题和答案
微信小程序·小程序·旅游
weixin_lynhgworld1 天前
陪诊小程序系统开发:开启就医陪伴新体验 [特殊字符]
小程序