Loading...
Loading...
Compare original and translation side by side
useScrolluseTransformuseScrolluseTransformuseScrollimport { useScroll, useTransform } from "framer-motion";
function Component() {
const { scrollYProgress } = useScroll();
return (
<motion.div
style={{ scaleX: scrollYProgress }}
/>
);
}useScrollimport { useScroll, useTransform } from "framer-motion";
function Component() {
const { scrollYProgress } = useScroll();
return (
<motion.div
style={{ scaleX: scrollYProgress }}
/>
);
}const { scrollYProgress } = useScroll({
target: ref,
offset: ["start end", "end start"],
container: containerRef
});const { scrollYProgress } = useScroll({
target: ref,
offset: ["start end", "end start"],
container: containerRef
});"start end"startcenterend| Combination | Meaning |
|---|---|
| While element visible |
| While scrolling down |
| Entire document scroll |
"start end"startcenterend| 组合 | 含义 |
|---|---|
| 元素可见期间 |
| 向下滚动期间 |
| 整个文档滚动 |
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 }} />;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 }} />;const x = useTransform(
scrollYProgress,
[0, 0.3, 0.6, 1],
[0, -50, 50, 0]
);const x = useTransform(
scrollYProgress,
[0, 0.3, 0.6, 1],
[0, -50, 50, 0]
);const x = useTransform(
scrollYProgress,
[0, 1],
[0, 100],
{ clamp: false }
);const x = useTransform(
scrollYProgress,
[0, 1],
[0, 100],
{ clamp: false }
);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>
);
}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>
);
}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%"
}}
/>
);
}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%"
}}
/>
);
}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>
);
}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>
);
}| Prop | Type | Default | Description |
|---|---|---|---|
| once | | | Only trigger once |
| margin | | | Offset for trigger |
| amount | | | Visibility threshold |
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| once | | | 仅触发一次 |
| margin | | | 触发偏移量 |
| amount | | | 可见性阈值 |
transformOrigintransformOriginuseInViewonce: trueonce: trueuseInView