Skip to content

Instantly share code, notes, and snippets.

View MijaelFV's full-sized avatar
😀
Studying

Mijael Eros Flores Vega MijaelFV

😀
Studying
  • Ushuaia, Argentina
View GitHub Profile
@coleturner
coleturner / framer-motion-use-viewport-scroll-with-element-container.md
Last active February 5, 2024 09:46
(Framer Motion): useViewportScroll with element container

Demo

Context

useViewportScroll is a great way to create a parallax effect as the page scrolls. In some cases however, we only want to scroll when an element is in the viewport area.

So for example, if we have a "landscape" scene, and want to animate the Sun object only when it's in view, we start with our useViewportScroll implementation:

function Sun(props) {
 const { scrollY, scrollYProgress } = useViewportScroll();