Skip to content

Scrollbar 滚动条(模拟)

介绍

通过触屏事件模拟的滚动条,解决小程序下 scroll-view 滚动状态获取、控制难的问题,功能与 Scroller 大致相同,在部分场景无法替代原 scroll-view,可根据场景选择使用。

基础用法

通过 @scroll 监听元素滚动的实时 scrollTopscrollLeft 变化。

html
<vd-scrollbar
  scroll-x
  scroll-y
  @scroll="handleScroll"
>
  内容内容内容...
</vd-scrollbar>
ts
import type { ScrollbarScrollEvent } from 'vital-design';

function handleScroll(info: ScrollbarScrollEvent) {
  console.log('滚动变化:', info);
}

滚动阴影

通过 shadow-xshadow-y 控制滚动条的阴影效果。

html
<vd-scrollbar scroll-y shadow-y>
  内容内容内容...
</vd-scrollbar>

<!-- 单独设置Y轴顶部的阴影效果 -->
<vd-scrollbar scroll-y shadow-y="top">
  内容内容内容...
</vd-scrollbar>

设置滚动位置

通过 lefttop 设置元素的滚动位置,你也可以通过组件提供的 scrollTo 方法设置滚动位置。

html
<vd-scrollbar
  ref="scrollbarRef"
  v-model:left="state.left"
  v-model:top="state.top"
  :scroll-y="true"
>
  内容内容内容...
</vd-scrollbar>
ts
import { easing, bezier } from '@vital-design/shared';

const scrollbarRef = shallowRef();

const state = reactive({
  left: 0,
  top: 0,
});

// 通过双向绑定设置
function scrollTo1() {
  state.left = 100;
  state.top = 100;
}

// 通过方法设置(支持自定义动画曲线)
function scrollTo2() {
  scrollbarRef.value.scrollTo({
    left: 100,
    top: 100,
    animation: true,
    // animation: easing('ease'),
    // animation: bezier(0.25, 0.1, 0.25, 1),
  });
}

滚动到元素

通过组件提供的 scrollToView 方法滚动到元素。

html
<vd-scrollbar ref="scrollbarRef" :scroll-y="true">
  <div id="element1">元素元素元素</div>
  <div id="element2">元素元素元素</div>
  <div id="element3">元素元素元素</div>
</vd-scrollbar>
ts
const scrollbarRef = shallowRef();
const instance = getCurrentInstance();

function scrollToView() {
  scrollbarRef.value.scrollToView({
    selector: '#element2',
    animation: true,
    instance,
  });
}

滚动回弹

通过 rebound-xrebound-y 控制滚动条的回弹效果。

html
<vd-scrollbar scroll-y rebound-y>
  内容内容内容...
</vd-scrollbar>

<!-- 单独设置Y轴顶部的回弹效果 -->
<vd-scrollbar scroll-y rebound-y="top">
  内容内容内容...
</vd-scrollbar>

函数调用

使用 useScroll 方法,可实时获取 Scrollbar 组件状态与操作组件。

html
<vd-scrollbar name="page" :scroll-y="true">
  内容内容内容...
</vd-scrollbar>
ts
// 指定标识名称
const { nearlyX, nearlyY, onScroll } = useScroll('page');

nearlyX; // 最近的横向滚动可滚动滚动条
nearlyY; // 最近的纵向滚动可滚动滚动条

// 滚动事件监听
onScroll(({ type, item, event }) => {
  if (event.type === 'scroll') {
    console.log('滚动:', event);
  }
  else if (event.type === 'updated') {
    console.log('更新:', event);
  }
  else if (event.type === 'scrolltoupper') {
    console.log('触顶部/左边:', event);
  }
  else if (event.type === 'scrolltolower') {
    console.log('触底部/右边:', event);
  }
});

自定义滑块样式

通过组件提供的 css 变量来自定义滑块样式。

html
<vd-scrollbar scroll-y custom-class="scrollbar-demo">
  内容内容内容...
</vd-scrollbar>
css
.scrollbar-demo {
  --vd-scrollbar-slider-size: 6px;
  --vd-scrollbar-slider-background: rgba(25, 137, 250, 0.3);
  --vd-scrollbar-slider-thumb-opacity: 1;
  --vd-scrollbar-slider-thumb-color: #1989fa;
}

API

Props

参数说明类型默认值
name标识名称string-
left横向滚动条位置string | number-
top纵向滚动条位置string | number-
debounce尺寸计算防抖时间string | numberfalse
animation在设置滚动条位置时使用动画过渡booleantrue
animation-executer动画过渡执行方式,可选值为 cssjsScrollbarAnimationExecuter'css'
duration动画时长string | number500
timing-function动画过渡曲线函数CssTimingFunction'ease'
momentum滑动滚动条滚动结束时使用惯性滚动booleantrue
scroll-x是否开启横向滚动booleanfalse
scroll-y是否开启纵向滚动booleanfalse
shadow-x横向滚动条阴影效果boolean | 'left' | 'right'false
shadow-y纵向滚动条阴影效果boolean | 'top' | 'bottom'false
rebound-x横向滚动条回弹效果boolean | 'left' | 'right'false
rebound-y纵向滚动条回弹效果boolean | 'top' | 'bottom'false
slider-x是否出现横向滑块booleantrue
slider-y是否出现竖向滑块booleantrue
slider-always是否一直显示滑块booleanfalse
upper-threshold距顶部/左边触发触顶阈值string | number50
lower-threshold距底部/右边触发触底阈值string | number50
content-class内容节点类名ClassValue-
content-style内容节点样式StyleValue-

代替原始 Props

参数说明类型默认值
custom-id根节点idstring-
custom-class根节点类名ClassValue-
custom-style根节点样式StyleValue-

Events

事件名说明回调参数
click点击时触发MouseEvent
scroll滚动时触发ScrollbarOnScrollEvent
resize滚动尺寸变化时触发ScrollbarOnResizeEvent
updated滚动数据更新时触发NodeScrollData
update:top纵向滚动条位置更新时触发number
update:left横向滚动条位置更新时触发number
scrolltoupper触顶时触发ScrollbarOnScrollThresholdEvent
scrolltolower触底时触发ScrollbarOnScrollThresholdEvent

Slots

名称说明
default自定义内容

Expose

方法名说明参数返回值
scrollTo滚动到指定位置ScrollbarScrollToOption-
scrollToView滚动到指定元素ScrollbarScrollToViewOption-

类型定义

组件导出以下类型定义:

ts
import type {
  ScrollbarScrollToOption,
  ScrollbarScrollToViewOption,
  ScrollbarOnResizeEvent,
  ScrollbarOnScrollEvent,
  ScrollbarOnScrollThresholdEvent,
} from 'vital-design';

赣ICP备2025061025号-1