Scroll Animation
将滚动与进度、视差、内容出现效果及主动定位连接。
适用场景
- 当界面需要以下行为时使用 Scroll Animation:将滚动与进度、视差、内容出现效果及主动定位连接。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/scroll-animation.json https://orvui.efu.me/r/smooth-scroll.json https://orvui.efu.me/r/scroll-progress.json https://orvui.efu.me/r/parallax.json https://orvui.efu.me/r/scroll-to.json https://orvui.efu.me/r/scroll-reveal.json
此示例还需要:
终端
pnpm add lucide-vue-next@^0.468.0首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
Smooth Scroll
smooth-scroll.vue提供 Lenis 滚动,并通过 useSmoothScroll 共享偏移、速度与进度。
packages/vue/src/motion/scroll-animation/examples/smooth-scroll.vue
<script setup lang="ts">
import { ArrowUp } from "lucide-vue-next";
import { ScrollTo, SmoothScroll } from "@/components/orvui/motion/scroll-animation";
const sections = Array.from({ length: 16 }, (_, index) => index + 1);
</script>
<template>
<SmoothScroll :root="false" class="h-64 w-full max-w-lg overflow-y-auto scrollbar-hide rounded-2xl border border-border bg-card">
<div class="space-y-3 p-4"><div v-for="section in sections" :key="section" class="rounded-lg bg-muted/60 px-3 py-4 text-sm text-muted-foreground">Section {{ section }}</div></div>
<ScrollTo :to="0" aria-label="Scroll to top" class="sticky bottom-3 left-[calc(100%-3rem)] z-10 grid size-9 place-items-center rounded-full border border-border bg-background/80 text-foreground backdrop-blur transition-colors hover:bg-background"><ArrowUp class="size-4" /></ScrollTo>
</SmoothScroll>
</template>
packages/vue/src/motion/scroll-animation/src/ScrollTo.vue
Scroll Progress
scroll-progress.vue将滚动进度呈现为横条或圆环,并可使用弹簧平滑数值。
packages/vue/src/motion/scroll-animation/examples/scroll-progress.vue
<script setup lang="ts">
import { onBeforeUnmount } from "vue";
import { useMotionValue } from "motion-v";
import { ScrollProgress } from "@/components/orvui/motion/scroll-animation";
const fraction = useMotionValue(0);
function track(event: Event) {
const viewport = event.currentTarget as HTMLElement;
fraction.set(viewport.scrollTop / Math.max(1, viewport.scrollHeight - viewport.clientHeight));
}
onBeforeUnmount(() => fraction.destroy());
</script>
<template>
<div class="relative w-full max-w-lg overflow-hidden rounded-2xl border border-border bg-card">
<ScrollProgress :progress="fraction" :fixed="false" :height="3" />
<div class="absolute right-3 top-3 z-10 rounded-full bg-background/70 p-1 backdrop-blur"><ScrollProgress variant="circle" :progress="fraction" :size="36" /></div>
<div class="h-64 overflow-y-auto scrollbar-hide" @scroll="track"><div class="space-y-3 p-4">
<div v-for="section in 18" :key="section" class="rounded-lg bg-muted/60 px-3 py-4 text-sm text-muted-foreground">Section {{ section }}</div>
</div></div>
</div>
</template>
packages/vue/src/motion/scroll-animation/src/ScrollProgress.vue
Parallax
parallax.vue根据内容位置与指定速度系数,沿任一坐标轴偏移内容。
packages/vue/src/motion/scroll-animation/examples/parallax.vue
<script setup lang="ts">
import { ref } from "vue";
import { Parallax } from "@/components/orvui/motion/scroll-animation";
const viewport = ref<HTMLElement | null>(null);
const layers = [
{ speed: -0.6, wrapper: "absolute inset-x-0 -top-1/4 h-[150%]", src: "https://picsum.photos/id/528/800/600", image: "size-full object-cover" },
{ speed: 0.5, wrapper: "absolute inset-0 grid place-items-center" },
{ speed: 0.9, wrapper: "absolute bottom-4 right-4", src: "https://picsum.photos/id/621/120/120", image: "size-12 rounded-full border-2 border-background object-cover shadow-lg" },
];
</script>
<template>
<div ref="viewport" class="relative h-[600px] w-full max-w-2xl overflow-y-auto scrollbar-hide rounded-2xl border border-border bg-card">
<div class="flex h-80 items-center justify-center text-sm text-muted-foreground">Scroll down ↓</div>
<div class="relative h-96 overflow-hidden">
<Parallax v-for="(layer, index) in layers" :key="index" :container="viewport" :speed="layer.speed" :class="layer.wrapper">
<img v-if="layer.src" :src="layer.src" alt="" :class="layer.image" />
<span v-else class="rounded-full bg-background/85 px-5 py-2 text-base font-medium text-foreground backdrop-blur">Parallax</span>
</Parallax>
</div>
<div class="flex h-80 items-center justify-center text-sm text-muted-foreground">↑ Scroll up</div>
</div>
</template>
packages/vue/src/motion/scroll-animation/src/Parallax.vue
Scroll To
scroll-to.vue将当前滚动容器移动到指定元素、选择器或数值偏移。
packages/vue/src/motion/scroll-animation/examples/scroll-to.vue
<script setup lang="ts">
import { useId } from "vue";
import { ScrollTo, SmoothScroll } from "@/components/orvui/motion/scroll-animation";
const instanceId = useId();
const sections = ["Intro", "Features", "Pricing", "FAQ"].map(label => ({ label, id: `${instanceId}-${label.toLowerCase()}` }));
</script>
<template>
<SmoothScroll :root="false" class="relative h-80 w-full max-w-lg overflow-y-auto scrollbar-hide rounded-2xl border border-border bg-card">
<nav class="sticky top-0 z-10 flex gap-1.5 border-b border-border bg-background/80 p-2 backdrop-blur">
<ScrollTo v-for="section in sections" :key="section.id" :to="`#${section.id}`" :offset="-48" class="rounded-full px-3 py-1 text-sm text-muted-foreground transition-colors hover:bg-muted hover:text-foreground">{{ section.label }}</ScrollTo>
</nav>
<section v-for="section in sections" :id="section.id" :key="section.id" class="flex h-64 items-center justify-center text-lg font-medium text-foreground">{{ section.label }}</section>
</SmoothScroll>
</template>
packages/vue/src/motion/scroll-animation/src/ScrollTo.vue
Scroll Reveal
scroll-reveal.vue内容越过视口边界时显示,可选择仅执行一次或每次执行。
packages/vue/src/motion/scroll-animation/examples/scroll-reveal.vue
<script setup lang="ts">
import { ref } from "vue";
import { ScrollReveal } from "@/components/orvui/motion/scroll-animation";
const viewport = ref<HTMLElement | null>(null);
const samples = ["Spring slide", "Blur in", "Staggered by delay", "Reveal once"];
</script>
<template>
<div ref="viewport" class="h-80 w-full max-w-lg overflow-y-auto scrollbar-hide rounded-2xl border border-border bg-card">
<div class="flex flex-col gap-16 p-6"><div class="text-center text-sm text-muted-foreground">Scroll ↓</div>
<ScrollReveal v-for="(label, index) in samples" :key="label" :root="viewport" :once="false" :delay="index * 0.05"
class="rounded-xl border border-border bg-muted/50 px-4 py-16 text-center text-base font-medium text-foreground">{{ label }}</ScrollReveal>
<div class="text-center text-sm text-muted-foreground">End</div>
</div>
</div>
</template>
packages/vue/src/motion/scroll-animation/src/ScrollReveal.vue
状态与事件
此示例组合没有声明成对的受控属性与 update 事件。使用已列出的属性、原生事件和插槽组合内容;不要为它添加未公开的 v-model。
API 参考
Smooth Scroll
ScrollTo
to?string | number | HTMLElementprops.targettarget?string | number | HTMLElement—offset?number—duration?number—disabled?booleanfalse插槽
default() => VNodeChild#defaultSmoothScroll
root?booleantruelerp?number0.1duration?number1.2orientation?"vertical" | "horizontal""vertical"wheelMultiplier?number1touch?booleanfalseenabled?booleantrue插槽
default() => VNodeChild#defaultScroll Progress
variant?"circle" | "bar""bar"progress?MotionValue<number>fallbackspring?booleantrueposition?"top" | "bottom""top"height?number2fixed?booleantruesize?number40thickness?number3Parallax
speed?number0.3axis?"x" | "y""y"container?null | HTMLElement | Ref<HTMLElement | null, HTMLElement | null>—spring?booleantrue插槽
default() => VNodeChild#defaultScroll To
ScrollTo
to?string | number | HTMLElementprops.targettarget?string | number | HTMLElement—offset?number—duration?number—disabled?booleanfalse插槽
default() => VNodeChild#defaultSmoothScroll
root?booleantruelerp?number0.1duration?number1.2orientation?"vertical" | "horizontal""vertical"wheelMultiplier?number1touch?booleanfalseenabled?booleantrue插槽
default() => VNodeChild#defaultScroll Reveal
y?number16blur?number8duration?number0.6delay?number0once?booleantrueamount?number | "some" | "all"0.3root?null | HTMLElement | Ref<HTMLElement | null, HTMLElement | null>—插槽
default() => VNodeChild#default无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Scroll Animation?
- 通过 to、target、offset、duration、disabled 配置 Scroll Animation;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 通过default 插槽提供内容,应用副作用由调用方管理。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
更新于
来源与许可 ↗MIT · 0.1.1