微信小程序-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_915918413 小时前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
说私域4 小时前
从品牌附庸到自我表达:定制开发开源AI智能名片S2B2C商城小程序赋能下的营销变革
人工智能·小程序
難釋懷4 小时前
第一个小程序
小程序
春哥的研究所4 小时前
可视化DIY小程序工具!开源拖拽式源码系统,自由搭建,完整的源代码包分享
小程序·开源·开源拖拽式源码系统·开源拖拽式源码·开源拖拽式系统
weixin_lynhgworld4 小时前
盲盒一番赏小程序技术实现方案:高并发与防作弊的平衡之道
小程序
今日热点6 小时前
小程序主体变更全攻略:流程、资料与异常处理方案
经验分享·微信·小程序·企业微信·微信公众平台·微信开放平台
鸭鸭梨吖10 小时前
微信小程序---下拉框
微信小程序·小程序
CRMEB定制开发11 小时前
CRMEB Pro版前端环境配置指南
前端·微信小程序·uni-app·商城源码·微信商城·crmeb
mon_star°13 小时前
搭建一款结合传统黄历功能的日历小程序
微信·微信小程序·小程序·微信公众平台
The_era_achievs_hero13 小时前
微信小程序91~100
微信小程序·小程序