2021-04-20 10:44:10 +08:00
|
|
|
## Scrollbar 滚动条
|
|
|
|
|
|
|
|
用于替换浏览器原生滚动条。
|
|
|
|
|
|
|
|
### 基础用法
|
|
|
|
|
|
|
|
:::demo 通过 `height` 属性设置滚动条高度,若不设置则根据父容器高度自适应。
|
|
|
|
|
|
|
|
```html
|
2021-08-26 12:46:26 +08:00
|
|
|
<template>
|
|
|
|
<el-scrollbar height="400px">
|
|
|
|
<p class="item" v-for="item in 20">{{ item }}</p>
|
|
|
|
</el-scrollbar>
|
|
|
|
</template>
|
2021-04-20 10:44:10 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
### 横向滚动
|
|
|
|
|
|
|
|
:::demo 当元素宽度大于滚动条宽度时,会显示横向滚动条。
|
|
|
|
|
|
|
|
```html
|
2021-08-26 12:46:26 +08:00
|
|
|
<template>
|
|
|
|
<el-scrollbar>
|
|
|
|
<div class="flex-content">
|
|
|
|
<p class="item" v-for="item in 50">{{ item }}</p>
|
|
|
|
</div>
|
|
|
|
</el-scrollbar>
|
|
|
|
</template>
|
2021-04-20 10:44:10 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
### 最大高度
|
|
|
|
|
|
|
|
:::demo 当元素高度超过最大高度,才会显示滚动条。
|
|
|
|
|
|
|
|
```html
|
|
|
|
<template>
|
|
|
|
<el-button @click="add">添加元素</el-button>
|
|
|
|
<el-button @click="delete">删除元素</el-button>
|
|
|
|
<el-scrollbar max-height="400px">
|
|
|
|
<p class="item" v-for="item in count">{{ item }}</p>
|
|
|
|
</el-scrollbar>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
2021-09-04 19:29:28 +08:00
|
|
|
count: 3,
|
2021-04-20 10:44:10 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
add() {
|
|
|
|
this.count++
|
|
|
|
},
|
|
|
|
delete() {
|
|
|
|
if (this.count > 0) {
|
|
|
|
this.count--
|
|
|
|
}
|
2021-09-04 19:29:28 +08:00
|
|
|
},
|
|
|
|
},
|
2021-04-20 10:44:10 +08:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
2021-07-05 09:10:50 +08:00
|
|
|
### 手动滚动
|
|
|
|
|
|
|
|
:::demo 通过使用 `setScrollTop` 与 `setScrollLeft` 方法,可以手动控制滚动条滚动。
|
|
|
|
|
|
|
|
```html
|
|
|
|
<template>
|
|
|
|
<el-scrollbar ref="scrollbar" height="400px" always>
|
|
|
|
<div ref="inner">
|
|
|
|
<p class="item" v-for="item in 20">{{ item }}</p>
|
|
|
|
</div>
|
|
|
|
</el-scrollbar>
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
<el-slider
|
|
|
|
v-model="value"
|
|
|
|
@input="inputSlider"
|
|
|
|
:max="max"
|
|
|
|
:format-tooltip="formatTooltip"
|
|
|
|
></el-slider>
|
2021-07-05 09:10:50 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
max: 0,
|
2021-09-04 19:29:28 +08:00
|
|
|
value: 0,
|
2021-07-05 09:10:50 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.max = this.$refs.inner.clientHeight - 380
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
inputSlider(value) {
|
|
|
|
this.$refs.scrollbar.setScrollTop(value)
|
|
|
|
},
|
|
|
|
formatTooltip(value) {
|
|
|
|
return `${value} px`
|
2021-09-04 19:29:28 +08:00
|
|
|
},
|
|
|
|
},
|
2021-07-05 09:10:50 +08:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
|
|
|
|
:::
|
|
|
|
|
2021-04-20 10:44:10 +08:00
|
|
|
### Scrollbar Attributes
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
| ---------- | -------------------------------------------------------------------- | --------------- | ------ | ------ |
|
|
|
|
| height | 滚动条高度 | string / number | — | — |
|
|
|
|
| max-height | 滚动条最大高度 | string / number | — | — |
|
|
|
|
| native | 是否使用原生滚动条样式 | boolean | — | false |
|
|
|
|
| wrap-style | 包裹容器的自定义样式 | string | — | — |
|
|
|
|
| wrap-class | 包裹容器的自定义类名 | string | — | — |
|
|
|
|
| view-style | 视图的自定义样式 | string | — | — |
|
|
|
|
| view-class | 视图的自定义类名 | string | — | — |
|
|
|
|
| noresize | 不响应容器尺寸变化,如果容器尺寸不会发生变化,最好设置它可以优化性能 | boolean | — | false |
|
|
|
|
| tag | 视图的元素标签 | string | — | div |
|
|
|
|
| always | 滚动条总是显示 | boolean | — | false |
|
|
|
|
| min-size | 滚动条最小尺寸 | number | — | 20 |
|
2021-04-20 10:44:10 +08:00
|
|
|
|
2021-07-05 09:10:50 +08:00
|
|
|
### Scrollbar Events
|
2021-04-20 10:44:10 +08:00
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
| 事件名称 | 说明 | 回调参数 |
|
|
|
|
| -------- | ---------------- | ---------------------------------- |
|
|
|
|
| scroll | 滚动时触发的事件 | 滚动距离 { scrollLeft, scrollTop } |
|
2021-07-05 09:10:50 +08:00
|
|
|
|
|
|
|
### Scrollbar Methods
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
| 方法名 | 说明 | 参数 |
|
|
|
|
| ------------- | ---------------------- | -------------------- |
|
|
|
|
| setScrollTop | 设置滚动条到顶部的距离 | (scrollTop: number) |
|
2021-07-22 11:18:34 +08:00
|
|
|
| setScrollLeft | 设置滚动条到左边的距离 | (scrollLeft: number) |
|
2021-09-04 19:29:28 +08:00
|
|
|
| update | 手动更新滚动条状态 | — |
|