当前位置:首页 > 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 事件可以监听滚动位置,获取滚动距离。

<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 事件可以实现上拉加载功能。

<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
分享给朋友:

相关文章

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性,或者使用第三方 UI 库…

uniapp删除

uniapp删除

卸载 uniapp 项目依赖 在项目根目录下执行以下命令,移除 node_modules 和依赖锁文件: rm -rf node_modules package-lock.json 如需清理全…

uniapp开发电视应用

uniapp开发电视应用

开发环境准备 确保已安装HBuilderX最新版本,这是uniapp官方推荐的开发工具。安装Node.js环境,用于依赖管理和打包构建。准备Android Studio或Xcode用于调试和打包TV应…

uniapp营销

uniapp营销

营销策略 采用社交媒体推广,结合微信、微博、抖音等平台进行内容营销。通过短视频、直播等形式展示产品特点,吸引用户关注。利用KOL合作,扩大品牌影响力,提高用户信任度。 用户裂变 设计裂变活动,如邀…

uniapp图文

uniapp图文

图文混排实现方式 在uniapp中实现图文混排通常采用<rich-text>组件或自定义样式布局。<rich-text>支持解析HTML字符串,适合动态内容渲染。 <te…

vue实现广告组件

vue实现广告组件

vue实现广告组件的方法 使用动态组件实现 在Vue中可以通过动态组件的方式加载广告组件,根据不同的条件展示不同的广告内容。动态组件使用<component :is="currentCompon…