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();