Tips for justifyContent using framer-motion's scrolling interpolation
P粉238433862
2023-08-31 18:42:45
<p>I have a piece of code that animates between x values, but it doesn't work when trying to transition from justify-content: center to left. </p>
<p>The following is a code snippet: </p>
<pre class="brush:php;toolbar:false;">function Navbar() {
const { scrollY } = useScroll();
const x = useTransform(scrollY, [0, 100], ["center", "left"]);
return (
<motion.div
layout
className={styles.parent}
style={{ justifyContent: x, display: "flex" }}
transition={{
duration: 0.5
}}
>
<Image
src="/BlackLogo-2.svg"
alt="Cg Logo"
width={100}
height={100}
style={{padding: 20,}}
priority
/>
</motion.div>
)
}</pre></p>
I found a solution. You just use the useMotionValueEvent function to check if the scroll is past a certain point and set it as state, then you have to wrap your child element (my image) in motion.div and also set the class in the outer div like below Show: