Skip to content

Scrollbar 滚动条

滚动条用于替换浏览器原生滚动条。VISTA B 直接使用 Element Plus 的 el-scrollbar,样式沿用组件库,适合列表、弹窗内容区、侧边栏和横向内容容器。

基础用法

通过 height 属性设置滚动区域高度。未设置 height 时,滚动条会根据父容器高度自适应。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vue
<el-scrollbar height="400px">
  <p v-for="item in 20" :key="item" class="scrollbar-demo-item">
    {{ item }}
  </p>
</el-scrollbar>

横向滚动

当内容宽度大于滚动容器宽度时,会显示横向滚动条。横向列表需要让内部内容保持一行展示,并设置内容宽度为 fit-content

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
vue
<el-scrollbar>
  <div class="scrollbar-flex-content">
    <p v-for="item in 50" :key="item" class="scrollbar-demo-item">
      {{ item }}
    </p>
  </div>
</el-scrollbar>

最大高度

通过 max-height 设置最大高度。当内容没有超过最大高度时不显示滚动条,超过后才会出现滚动条。

1
2
3
vue
<template>
  <el-button @click="add">Add Item</el-button>
  <el-button @click="onDelete">Delete Item</el-button>
  <el-scrollbar max-height="400px">
    <p v-for="item in count" :key="item" class="scrollbar-demo-item">
      {{ item }}
    </p>
  </el-scrollbar>
</template>

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

const count = ref(3)
const add = () => count.value++
const onDelete = () => {
  if (count.value > 0) count.value--
}
</script>

手动滚动

通过 scrollTosetScrollTopsetScrollLeft 方法,可以主动控制滚动位置。需要先通过 ref 获取 el-scrollbar 实例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vue
<template>
  <el-scrollbar ref="scrollbarRef" height="400px" always>
    <p v-for="item in 20" :key="item" class="scrollbar-demo-item">
      {{ item }}
    </p>
  </el-scrollbar>
  <el-button @click="scrollbarRef.setScrollTop(120)">Scroll Top</el-button>
</template>

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

const scrollbarRef = ref()
</script>

无限滚动

end-reached 会在滚动条到达边缘时触发,可用于加载更多。配合 distance 可以设置距离边缘多少像素时触发。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
vue
<template>
  <el-scrollbar height="400px" :distance="20" @end-reached="loadMore">
    <p v-for="item in count" :key="item" class="scrollbar-demo-item">
      {{ item }}
    </p>
  </el-scrollbar>
</template>

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

const count = ref(30)
const loadMore = (direction) => {
  if (direction === "bottom") count.value += 5
}
</script>

API

Scrollbar 属性

属性说明类型默认值
height滚动条高度string / number
max-height滚动条最大高度string / number
native是否使用原生滚动条样式booleanfalse
wrap-style包裹容器自定义样式string / object / array
wrap-class包裹容器自定义类名string / array
view-style视图自定义样式string / object / array
view-class视图自定义类名string / array
noresize不响应容器尺寸变化,可用于尺寸固定场景的性能优化booleanfalse
tag视图元素标签stringdiv
always是否总是显示滚动条booleanfalse
min-size滚动条最小尺寸number20
id视图 IDstring
role视图角色string
aria-label视图 aria-labelstring
aria-orientation视图 aria-orientationhorizontal / vertical / undefined
tabindex包裹容器 tabindexstring / number
distance触发 end-reached 的边缘距离,单位 pxnumber0

Scrollbar 事件

事件说明类型
scroll滚动时触发,返回 scrollTopscrollLeftFunction
end-reached滚动到边缘时触发,返回方向 top / bottom / left / rightFunction

Scrollbar 插槽

插槽说明
default自定义默认内容

Scrollbar 暴露方法

名称说明
handleScroll触发滚动事件
scrollTo滚动到指定坐标
setScrollTop设置滚动条到顶部的距离
setScrollLeft设置滚动条到左侧的距离
update手动更新滚动条状态
wrapRef滚动条包裹容器 ref

HOMEVISTA 设计规范