P粉2933419692023-09-01 09:20:19
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:
function Navbar() { const { scrollY } = useScroll(); const [Scrolled, setScrolled] = useState(false); useMotionValueEvent(scrollY, "change", (latest) => { if (latest > 200) { setScrolled(true); } else { setScrolled(false); } }) return ( <div style={{justifyContent: Scrolled? "left" : "center"}} className={styles.icon} > <motion.div layout transition={{type: "spring", stiffness: 700, damping: 30}} > <Image src="/BlackLogo-2.svg" alt="Cg Logo" width={100} height={100} style={{padding: 20,}} priority /> </motion.div> </div> ) }