当前位置:首页 > uni-app

uniapp滚动组件

2026-03-05 05:58:48uni-app

uniapp 滚动组件实现方法

scroll-view 基础用法

uniapp 提供了 scroll-view 组件实现滚动区域。横向滚动需要设置 scroll-x 属性,纵向滚动需要设置 scroll-y 属性。

<scroll-view scroll-y style="height: 300px;">
  <view v-for="item in 50" :key="item">列表项 {{item}}</view>
</scroll-view>

滚动事件监听

通过 @scroll 事件可以监听滚动位置,获取滚动距离。

uniapp滚动组件

<scroll-view 
  scroll-y 
  style="height: 300px;"
  @scroll="handleScroll"
>
  <!-- 内容 -->
</scroll-view>
methods: {
  handleScroll(e) {
    console.log('滚动距离', e.detail.scrollTop)
  }
}

滚动到指定位置

使用 scroll-into-view 属性可以滚动到指定子元素位置。

<scroll-view 
  scroll-y 
  style="height: 300px;"
  :scroll-into-view="toView"
>
  <view id="item1">项目1</view>
  <view id="item30">项目30</view>
</scroll-view>
<button @click="toView = 'item30'">滚动到项目30</button>
data() {
  return {
    toView: ''
  }
}

上拉加载更多

结合 @scrolltolower 事件可以实现上拉加载功能。

uniapp滚动组件

<scroll-view
  scroll-y
  style="height: 300px;"
  @scrolltolower="loadMore"
>
  <!-- 列表内容 -->
  <view class="loading">加载中...</view>
</scroll-view>

下拉刷新

使用 refresher-enabled 开启下拉刷新功能。

<scroll-view
  scroll-y
  style="height: 300px;"
  refresher-enabled
  :refresher-triggered="refreshing"
  @refresherrefresh="onRefresh"
>
  <!-- 内容 -->
</scroll-view>
data() {
  return {
    refreshing: false
  }
},
methods: {
  onRefresh() {
    this.refreshing = true
    setTimeout(() => {
      this.refreshing = false
    }, 1000)
  }
}

性能优化建议

对于长列表场景,建议配合 uvuerecycle-list 组件使用,减少内存占用。

<recycle-list 
  for="item in longList"
  scroll-y
  style="height: 100vh;"
>
  <cell-slot>
    <view>{{item.text}}</view>
  </cell-slot>
</recycle-list>

标签: 组件uniapp
分享给朋友:

相关文章

uniapp 极光推送

uniapp 极光推送

uniapp 集成极光推送的方法 在 uniapp 中集成极光推送,需要使用官方提供的插件或自行封装原生模块。以下是具体实现方式: 使用官方插件 在 uni-app 插件市场搜索「极光推送」插件,…

uniapp隐藏滚动条

uniapp隐藏滚动条

在UniApp中隐藏滚动条可以通过以下几种方法实现,具体根据使用场景选择: 全局样式设置(APP/H5) 通过修改全局CSS样式隐藏滚动条,适用于所有页面: ::-webkit-scrollbar…

uniapp树形选择

uniapp树形选择

uniapp树形选择实现方法 使用uni-data-checkbox组件 uniapp内置的uni-data-checkbox组件支持树形结构选择,适用于多级分类场景。配置时需要将数据格式化为树形结构…

uniapp使用axios无法请求

uniapp使用axios无法请求

uniapp中使用axios请求问题解决方案 在uniapp中使用axios可能会遇到跨域、请求失败或兼容性问题。以下是常见原因及解决方法: 检查axios安装与引入 确保已正确安装axios:…

uniapp悬浮

uniapp悬浮

实现悬浮效果的方法 在UniApp中实现悬浮效果可以通过CSS的position: fixed或position: sticky属性结合动态样式控制来实现。以下是具体实现方式: 使用CSS固…

丸子uniapp

丸子uniapp

丸子UniApp简介 丸子UniApp是一个基于UniApp框架开发的移动应用解决方案,通常用于快速构建跨平台应用(如微信小程序、H5、Android/iOS等)。其核心特点是利用UniApp的“一次…