framer-motion-scroll
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFramer Motion Scroll Animations
Framer Motion 滚动动画
When to Use This Skill
何时使用该技能
Apply when implementing scroll-driven animations: using , , scroll-linked effects, parallax, or progress indicators. When the user asks about scroll animation in Framer Motion, recommend Framer Motion's scroll utilities.
useScrolluseTransformRelated skills: For core animation use framer-motion-core; for variants use framer-motion-variants; for layout animations use framer-motion-layout.
在实现滚动驱动动画时使用:包括使用、、滚动关联效果、视差或进度指示器。当用户询问Framer Motion中的滚动动画相关问题时,推荐使用Framer Motion的滚动工具。
useScrolluseTransform相关技能: 核心动画使用framer-motion-core;变体使用framer-motion-variants;布局动画使用framer-motion-layout。
useScroll
useScroll
useScrolljsx
import { useScroll, useTransform } from "framer-motion";
function Component() {
const { scrollYProgress } = useScroll();
return (
<motion.div
style={{ scaleX: scrollYProgress }}
/>
);
}useScrolljsx
import { useScroll, useTransform } from "framer-motion";
function Component() {
const { scrollYProgress } = useScroll();
return (
<motion.div
style={{ scaleX: scrollYProgress }}
/>
);
}useScroll Options
useScroll 配置选项
jsx
const { scrollYProgress } = useScroll({
target: ref,
offset: ["start end", "end start"],
container: containerRef
});jsx
const { scrollYProgress } = useScroll({
target: ref,
offset: ["start end", "end start"],
container: containerRef
});Offset Format
偏移量格式
"start end"startcenterend| Combination | Meaning |
|---|---|
| While element visible |
| While scrolling down |
| Entire document scroll |
"start end"startcenterend| 组合 | 含义 |
|---|---|
| 元素可见期间 |
| 向下滚动期间 |
| 整个文档滚动 |
useTransform
useTransform
Map one motion value to another:
jsx
const { scrollYProgress } = useScroll();
const scale = useTransform(scrollYProgress, [0, 1], [1, 2]);
const opacity = useTransform(scrollYProgress, [0, 0.5, 1], [0, 1, 0]);
return <motion.div style={{ scale, opacity }} />;将一个运动值映射到另一个:
jsx
const { scrollYProgress } = useScroll();
const scale = useTransform(scrollYProgress, [0, 1], [1, 2]);
const opacity = useTransform(scrollYProgress, [0, 0.5, 1], [0, 1, 0]);
return <motion.div style={{ scale, opacity }} />;Multi-step Transforms
多阶段转换
jsx
const x = useTransform(
scrollYProgress,
[0, 0.3, 0.6, 1],
[0, -50, 50, 0]
);jsx
const x = useTransform(
scrollYProgress,
[0, 0.3, 0.6, 1],
[0, -50, 50, 0]
);Disable Clamping
禁用限制
jsx
const x = useTransform(
scrollYProgress,
[0, 1],
[0, 100],
{ clamp: false }
);jsx
const x = useTransform(
scrollYProgress,
[0, 1],
[0, 100],
{ clamp: false }
);Parallax Effect
视差效果
Different scroll speeds for depth:
jsx
function ParallaxSection() {
const { scrollYProgress } = useScroll({
target: ref,
offset: ["start end", "end start"]
});
const y = useTransform(scrollYProgress, [0, 1], [100, -100]);
return (
<motion.div ref={ref} style={{ y }}>
Parallax Content
</motion.div>
);
}通过不同滚动速度营造深度感:
jsx
function ParallaxSection() {
const { scrollYProgress } = useScroll({
target: ref,
offset: ["start end", "end start"]
});
const y = useTransform(scrollYProgress, [0, 1], [100, -100]);
return (
<motion.div ref={ref} style={{ y }}>
Parallax Content
</motion.div>
);
}Scroll Progress Bar
滚动进度条
jsx
function ScrollProgress() {
const { scrollYProgress } = useScroll();
return (
<motion.div
style={{
scaleX: scrollYProgress,
position: "fixed",
top: 0,
left: 0,
right: 0,
height: 4,
backgroundColor: "#00ff00",
transformOrigin: "0%"
}}
/>
);
}jsx
function ScrollProgress() {
const { scrollYProgress } = useScroll();
return (
<motion.div
style={{
scaleX: scrollYProgress,
position: "fixed",
top: 0,
left: 0,
right: 0,
height: 4,
backgroundColor: "#00ff00",
transformOrigin: "0%"
}}
/>
);
}useInView with Variants
useInView 与变体
Trigger animation when entering viewport:
jsx
import { useInView, motion } from "framer-motion";
import { useRef } from "react";
function FadeInSection() {
const ref = useRef(null);
const isInView = useInView(ref, { once: true, margin: "-100px" });
return (
<motion.div
ref={ref}
initial={{ opacity: 0, y: 50 }}
animate={isInView ? { opacity: 1, y: 0 } : { opacity: 0, y: 50 }}
transition={{ duration: 0.6 }}
>
Content
</motion.div>
);
}进入视口时触发动画:
jsx
import { useInView, motion } from "framer-motion";
import { useRef } from "react";
function FadeInSection() {
const ref = useRef(null);
const isInView = useInView(ref, { once: true, margin: "-100px" });
return (
<motion.div
ref={ref}
initial={{ opacity: 0, y: 50 }}
animate={isInView ? { opacity: 1, y: 0 } : { opacity: 0, y: 50 }}
transition={{ duration: 0.6 }}
>
Content
</motion.div>
);
}useInView Options
useInView 配置选项
| Prop | Type | Default | Description |
|---|---|---|---|
| once | | | Only trigger once |
| margin | | | Offset for trigger |
| amount | | | Visibility threshold |
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| once | | | 仅触发一次 |
| margin | | | 触发偏移量 |
| amount | | | 可见性阈值 |
Best practices
最佳实践
- ✅ Use useScroll with useTransform for scroll-linked animations.
- ✅ Use whileInView for simple scroll-triggered animations.
- ✅ Use proper offset values to control start/end.
- ✅ Use for scale animations.
transformOrigin
- ✅ 将useScroll与useTransform配合使用,实现滚动关联动画。
- ✅ 使用whileInView实现简单的滚动触发动画。
- ✅ 使用合适的offset值控制动画的开始/结束时机。
- ✅ 为缩放动画设置。
transformOrigin
Do Not
注意事项
- ❌ Use scroll animations without offset values.
- ❌ Forget to set height on scroll containers for horizontal scroll.
- ❌ Animate layout properties for scroll effects.
- ❌ Use without
useInViewfor entrance animations.once: true
- ❌ 不要在没有设置偏移量的情况下使用滚动动画。
- ❌ 不要忘记为水平滚动的滚动容器设置高度。
- ❌ 不要为滚动效果动画布局属性。
- ❌ 不要在入场动画中使用未设置的
once: true。useInView