Skip to content

如何使用

基础使用

只需要将组件包裹你的需要滚动内容,设置好容器大小,内容将自动允许滚动并设置滚动条(默认垂直滚动)。

TIP

滚动容器撑开父级,因此在未设置高度或者最大高度时,不会出现滚动条,需要设置一个确定的高度。

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

滚动区域自动撑开

某些时候,你可能希望滚动区域在很小时自动撑开容器,超出指定高度时才开始滚动,则可以设置 maxHeight 或者 maxWidth 属性, 当内容高度超出设置的最大值之后,才开始滚动。

滚动方向

可以通过 scroll 属性设置仅垂直滚动或仅水平滚动,或者二者都滚动。

  • both : 水平滚动和垂直滚动
  • vertical : 垂直滚动
  • horizontal : 水平滚动
  • none : 禁止滚动

scroll: both

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

scroll: vertical

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

scroll: horizontal

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

scroll: none

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

下拉刷新

在滚动内容顶部继续向下拖动可触发刷新(指针:触摸或鼠标)。需将 scroll 设为 verticalboth;仅 horizontal 时不会启用。

开启 enablePullToRefresh 后,当 scrollTop === 0 时下拉,松手时若下拉距离不小于 pullToRefreshThreshold,会触发 pullToRefreshStart。请使用 v-model:pullToRefreshLoading 表示加载中:开始刷新时设为 true,数据就绪后设为 false;从加载态结束时组件会触发 pullToRefreshEnd

TIP

pullToRefreshStart 中请 尽快(同步)pullToRefreshLoading 设为 true,再执行异步请求,可避免顶部加载区域短暂闪烁。

属性说明
enablePullToRefresh是否启用下拉刷新,默认 false
pullToRefreshThreshold触发刷新所需的最小下拉距离(像素),默认 50
pullToRefreshLoading是否处于加载中,支持 v-model:pullToRefreshLoading,默认 false
pullRefreshLoadingHeight加载时顶部提示区域高度(像素),默认 48
pullRefreshMaxDistance最大下拉距离与阻尼参考(像素),默认 120
事件说明
pullToRefreshStart达到阈值并松手,应在此处开始刷新
pullToRefreshEndpullToRefreshLoadingtrue 变为 false 后触发

默认会显示「下拉加载 / 释放加载 / 加载中…」文案;可通过插槽 pullRefresh 完全自定义。插槽作用域参数:

名字类型说明
state'idle' | 'pulling' | 'release' | 'loading'当前状态
distancenumber当前下拉距离(像素)
thresholdnumberpullToRefreshThreshold 相同
loadingbooleanpullToRefreshLoading 相同
loadingHeightnumberpullRefreshLoadingHeight 相同

可通过 CSS 变量调整默认样式,例如 --vue-scroll-rect-pull-refresh-text-color--vue-scroll-rect-pull-refresh-font-size--vue-scroll-rect-pull-refresh-spinner-color

在顶部向下拖动即可触发刷新(约 1.5 秒模拟请求)

列表行 1

列表行 2

列表行 3

列表行 4

列表行 5

列表行 6

列表行 7

列表行 8

列表行 9

列表行 10

列表行 11

列表行 12

列表行 13

列表行 14

列表行 15

列表行 16

列表行 17

列表行 18

列表行 19

列表行 20

列表行 21

列表行 22

列表行 23

列表行 24

自定义下拉区域

vue
<template>
  <ScrollRect
    scroll="vertical"
    :enable-pull-to-refresh="true"
    v-model:pull-to-refresh-loading="loading"
    @pull-to-refresh-start="onPullRefresh"
  >
    <template #pullRefresh="{ state, distance, threshold, loading: isLoading }">
      <div class="my-pull">
        <span v-if="isLoading">刷新中…</span>
        <span v-else-if="state === 'release'">松开刷新</span>
        <span v-else>下拉 {{ Math.round(distance) }} / {{ threshold }} px</span>
      </div>
    </template>
    <p v-for="n in 10" :key="n">内容 {{ n }}</p>
  </ScrollRect>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { ScrollRect } from '@imengyu/vue-scroll-rect';

const loading = ref(false);

async function onPullRefresh() {
  loading.value = true;
  await new Promise<void>((r) => setTimeout(r, 800));
  loading.value = false;
}
</script>

返回顶部

设置 enableBackToToptrue 后,会在滚动容器内提供返回顶部区域(默认按钮或插槽)。仅当滚动偏移超过 backToTopThreshold才显示:vertical / bothscrollTophorizontal / bothscrollLeft。点击后将滚动位置设回起点 (0, 0),等价于实例方法 scrollToStart()

默认按钮使用 opacity + translateY 渐变显隐(可通过 CSS 变量调整时长与位移)。也可通过模板 ref 调用 scrollToStart() 自行放置按钮。

属性说明
enableBackToTop是否启用返回顶部区域(默认按钮或 backToTop 插槽),默认 false
backToTopThreshold超过该偏移量(像素)后显示按钮;both 时任一轴向超过即显示,默认 120

插槽 backToTop 可完全自定义按钮;作用域参数:

名字类型说明
onBackToTop() => void调用后滚动到起点,与 scrollToStart 一致
visibleboolean是否应显示(与默认按钮显隐逻辑一致,便于自定义动画)

默认按钮样式可通过 CSS 变量覆盖:--vue-scroll-rect-back-to-top-bg--vue-scroll-rect-back-to-top-bg-hover--vue-scroll-rect-back-to-top-color--vue-scroll-rect-back-to-top-transition-duration--vue-scroll-rect-back-to-top-translate-y

向下滚动后点击右下角按钮可回到顶部

列表行 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

自定义返回顶部

vue
<template>
  <ScrollRect scroll="vertical" :enable-back-to-top="true" containerClass="content">
    <template #backToTop="{ onBackToTop, visible }">
      <button
        type="button"
        class="my-back-top"
        :class="{ 'is-on': visible }"
        @click="onBackToTop"
      >
        ↑ Top
      </button>
    </template>
    <p v-for="n in 20" :key="n">内容 {{ n }}</p>
  </ScrollRect>
</template>

<script setup lang="ts">
import { ScrollRect } from '@imengyu/vue-scroll-rect';
</script>

<style lang="scss" scoped>
.my-back-top {
  position: absolute;
  right: 16px;
  bottom: 16px;
  z-index: 2;
  padding: 8px 12px;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.my-back-top.is-on {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
</style>

滚动条

滚动条样式

可以通过 css 修改滚动条样式颜色等等,有一些 css 变量可以用于修改滚动条样式。

名字说明
--vue-scroll-rect-scrollbar-thumb-color滚动条滑块的颜色
--vue-scroll-rect-scrollbar-thumb-color-light滚动条滑块鼠标悬浮时的颜色
--vue-scroll-rect-scrollbar-thumb-color-pressed滚动条滑块鼠标按下时的颜色
--vue-scroll-rect-scrollbar-thumb-radius滚动条滑块半径
--vue-scroll-rect-scrollbar-thumb-margin滚动条左右边距
--vue-scroll-rect-scrollbar-size滚动条宽度

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

滚动条常显示

滚动条默认是在鼠标移入后渐变显示,你可以设置 scrollBarAlwaysShow 属性为 true ,滚动条将一直显示。

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

设置 scrollBarBackgroundClickable 属性为 true ,滚动条背景点击区域也可触发滚动。

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

Long content that can scroll

Hello. This is a simple scroll rect component for Vue3. You can wrap your long content with this component. So it can scroll.

It is easy to use!

It provides a simple scrollbar that can fade out and help you unify the display style of each browser.

It is easy to use!

更换内置的滚动条

如果你希望换掉内置的滚动条,使用自己的滚动条组件,可以通过插槽 scrollBarXscrollBarY 来实现。

插槽会传出当前滚动条滚动位置和计算长度数据 (scrollBarValue),你可以根据这个百分比来计算滚动条的显示位置;传出 onScroll 回调用于控制滚动。。

scrollBarValue 结构如下:

名字类型类型
showboolean是否显示滚动条
sizenumber滚动条计算长度(百分比,0-100)
sizeRawnumber滚动条计算长度(像素)
posnumber滚动条滚动位置(百分比,0-100)

onScroll 结构如下:

参数类型
posnumber
vue
<template>
  <ScrollRect class="long-content" scroll="both">
    <ScrollContent />
    <template #scrollBarY="{ scrollBarValue, onScroll }">
      <MyScrollBar :value="scrollBarValue.pos" :size="scrollBarValue.size" @update:value="onScroll" />
    </template>
  </ScrollRect>
</template>

本项目使用 MIT 开源协议