Skip to content

滚动指令

滚动指令帮助您管理滚动行为和无限加载。

v-scroll

跟踪滚动位置和方向。

基本用法

vue
<template>
  <div v-scroll="handleScroll" class="scroll-container">
    滚动内容
  </div>
</template>

<script setup>
function handleScroll(event, info) {
  console.log('滚动位置:', info.scrollTop)
  console.log('进度:', info.progressY)
  console.log('方向:', info.directionY)
}
</script>

滚动信息对象

处理程序接收详细的滚动信息:

属性类型描述
scrollTopnumber当前垂直滚动位置
scrollLeftnumber当前水平滚动位置
progressYnumber垂直滚动进度 (0-1)
progressXnumber水平滚动进度 (0-1)
directionY-1 | 0 | 1垂直方向 (-1: 上, 1: 下)
directionX-1 | 0 | 1水平方向 (-1: 左, 1: 右)

API

选项类型默认值描述
handlerFunction-滚动事件处理程序(必填)
throttlenumber0节流时间(毫秒)
passivebooleantrue使用被动事件监听
containerstring | Element-自定义滚动容器

v-infinite-scroll

列表和信息流的无限滚动加载。

基本用法

vue
<template>
  <div v-infinite-scroll="loadMore" class="scroll-container">
    <div v-for="item in items" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const items = ref([...])

async function loadMore() {
  const newItems = await fetchMoreItems()
  items.value.push(...newItems)
}
</script>

API

选项类型默认值描述
handlerFunction-滚动到底部时的处理程序
distancenumber0触发加载的距离(像素)
disabledbooleanfalse禁用无限滚动
loadingbooleanfalse加载状态
throttlenumber200节流时间(毫秒)

v-sticky

滚动时使元素保持粘性。

基本用法

vue
<template>
  <div v-sticky>粘性头部</div>
  <div v-sticky="50">距顶部 50px 的粘性</div>
  <div v-sticky="{ top: 60, zIndex: 1000 }">自定义粘性</div>
</template>

API

选项类型默认值描述
topnumber | string0粘性时的顶部偏移
zIndexnumber100粘性时的 z-index
stickyClassstring'v-sticky--fixed'粘性时的 CSS 类
onChangeFunction-粘性状态改变时的回调

基于 MIT 许可发布