Skip to content

UseScrollView

功能描述

UseScrollView 是一个用于管理滚动视图的 Vue 组合式函数,支持平滑滚动到指定元素、滚动方向控制、鼠标滚轮支持以及滚动状态样式管理。适用于长列表导航、内容区域滚动控制、分步表单等场景。

安装

bash
# 使用 pnpm
pnpm add @oiij/use

# 使用 npm
npm install @oiij/use

# 使用 yarn
yarn add @oiij/use

依赖

  • vue: ^3.0.0
  • @vueuse/core: ^10.0.0

基本使用

loading

API

useScrollView(templateRef, options?)

滚动视图控制。

参数

参数类型说明
templateRefMaybeRefOrGetter<HTMLElement | undefined>目标滚动容器的模板引用
optionsScrollViewOptions配置选项

ScrollViewOptions

选项类型默认值说明
activeClassNamestring'.active'激活元素的 CSS 选择器
enableWheelbooleantrue是否启用滚轮滚动
direction'horizontal' | 'vertical''vertical'滚动方向
scrollOffsetnumber-滚动偏移量
debounceDelaynumber500滚动防抖延迟时间(毫秒)
autoScrollOnResizebooleantrue是否在容器尺寸变化时自动滚动
scrollIntoViewOptionsScrollIntoViewOptions-scrollIntoView 配置选项

返回值

属性类型说明
scrollToView(options?)Function滚动到激活元素
scrollToElement(element, options?)Function滚动到指定元素
scrollToNext()Function滚动到下一个元素
scrollToPrevious()Function滚动到上一个元素
getActiveElement()Function获取激活元素
getScrollableElements()Function获取可滚动元素列表

类型定义

ts
export type ScrollDirection = 'horizontal' | 'vertical'

export type ScrollViewOptions = {
  /**
   * 激活元素的 CSS 选择器
   * @default '.active'
   */
  activeClassName?: string
  /**
   * 是否启用滚轮滚动
   * @default true
   */
  enableWheel?: boolean
  /**
   * 滚动方向
   * @default 'vertical'
   */
  direction?: ScrollDirection
  /**
   * 滚动偏移量
   */
  scrollOffset?: number
  /**
   * 滚动防抖延迟时间(毫秒)
   * @default 500
   */
  debounceDelay?: number
  /**
   * 是否在容器尺寸变化时自动滚动到激活元素
   * @default true
   */
  autoScrollOnResize?: boolean
  /**
   * scrollIntoView 的默认配置选项
   */
  scrollIntoViewOptions?: ScrollIntoViewOptions
}

export type UseScrollViewReturns = {
  scrollToView: (customOptions?: ScrollIntoViewOptions) => Promise<void>
  scrollToElement: (element: Element, customOptions?: ScrollIntoViewOptions) => Promise<void>
  scrollToNext: () => Promise<void>
  scrollToPrevious: () => Promise<void>
  getActiveElement: () => Element | null
  getScrollableElements: () => Element[]
}

export declare function useScrollView(templateRef: MaybeRefOrGetter<HTMLElement | undefined>, options?: ScrollViewOptions): UseScrollViewReturns

使用示例

基础用法

vue
<script setup>
import { useScrollView } from '@oiij/use'
import { ref } from 'vue'

const containerRef = ref()
const { scrollToView, scrollToNext, scrollToPrevious } = useScrollView(containerRef, {
  activeClassName: '.active',
  enableWheel: true,
  direction: 'vertical'
})
</script>

<template>
  <div ref="containerRef">
    <div class="item">
      Item 1
    </div>
    <div class="item active">
      Item 2
    </div>
    <div class="item">
      Item 3
    </div>
  </div>
  <button @click="scrollToPrevious">
    上一个
  </button>
  <button @click="scrollToNext">
    下一个
  </button>
</template>

水平滚动

ts
import { useScrollView } from '@oiij/use'

const { scrollToNext, scrollToPrevious } = useScrollView(containerRef, {
  direction: 'horizontal',
  activeClassName: '.active'
})

自定义滚动偏移

ts
import { useScrollView } from '@oiij/use'

const { scrollToNext } = useScrollView(containerRef, {
  scrollOffset: 200 // 每次滚动 200px
})