微信小程序-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的高度
相关推荐
fakaifa3 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
Dignity_呱8 小时前
如何在不发版时,实现小程序的 AB 测试?
前端·面试·微信小程序
说私域9 小时前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序
fakaifa17 小时前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_915918411 天前
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·东方仙盟·蔬菜批发·批发系统