Next.js Discord

Discord Forum

Need help implementing scroll animation

Answered
Anay-208 posted this in #help-forum
Open in Discord
I'm using a client component for scroll animation per word. Like this:
            <div ref={textRef} className="mx-4 text-center">

                    {words.map((word, index) => (
                        <span className=" text-black/50  text-3xl transition-all duration-500" key={index}>
                            {word}{" "}
                        </span>
                    ))}
            </div>

I'm using IntersectionObserver api to detect when the user scrolls.

Now what I want is that, when the user scrolls to this point in the page, the element should just stick to the top while I'm scrolling. Using fixed is not the smoothest
Answered by Anay-208
So, here's what i found:
- use sticky positioning for the thing I wanted
- use framer-motion for motion. its much easier to work with
View full answer

7 Replies

Holland Lop
you should pass a function to intersection observer to set top-0 when element is intersecting something like this:
element.style.top = 0;
@Holland Lop you should pass a function to intersection observer to set top-0 when element is intersecting something like this: element.style.top = 0;
I'm doing a similar one right now, but I also want it such that when the user is scrolling out of the component, it also disappears.
and this is also not that smooth
I'm actually making this all custom. Should I use any library for it?
Tonkinese
I might be out of date, but I still use gsap which has some great utility methods, but in the end... they're just utilities. They're not magic. I can see why someone might want to use an intersection observer to handle unneccesary computations for things that aren't in the viewport, but the way I would handle it would be to create an onScroll listener that's attached to the document or the parent that scrolls, and just use the observer to append a class that would be used in the scroll listener to get the items that are relevant at the time with a parent.getElementsByClassName... method. Then map through each element and use that elements getBoundingClientRect method to set whatever properties you need.
So, here's what i found:
- use sticky positioning for the thing I wanted
- use framer-motion for motion. its much easier to work with
Answer