question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Animate elements based on container rather than page

See original GitHub issue

When I want to animate a specific element in a specific section of a page, I have to create a really long-winded code solution to allow me to do so.

For example, I have to use the viewportscroll whilst calculating my sections offset from the top of the page and my sections height too, to be able to work out when to run the desired animation on an element either inside the section or on the page in general.

In short: Im wanting to tie an animation to a specific section of my page.

Heres my current code solution that is working - but it is messy and constantly means I have to repeat the code for every component:

    const ref = useRef();
    const [elementTop, setElementTop] = useState(null);
    const [elementBottom, setElementBottom] = useState(null);
    const [elementHeight, setElementHeight]= useState(null);
    
    useEffect(() => {
        if (!ref.current) return;
    
        function setValues() {
            setElementTop(ref.current.offsetTop);
            setElementHeight(ref.current.offsetHeight);
            setElementBottom(elementTop + elementHeight);
            scrollY.current = scrollY.current + elementHeight;
        };
    
        setValues();
        document.addEventListener("load", setValues);
        window.addEventListener("resize", setValues);   
       
    },[elementTop, elementBottom, elementHeight,]);
    
    let start = elementTop - elementHeight;
    let end = elementBottom - elementHeight;

    const closedRotate = useTransform(scrollY, [start, end], [0, -20], { clamp: true });
    const closedPosL = useTransform(scrollY, [start, end], [200, -300], { clamp: true });
    const closedPosY = useTransform(scrollY, [start, end], [-100, 300], { clamp: true });

I would like a solution that is written the same way as useElementScroll - as i was unaware that useElementScroll was only made for a scrollable (overflowed) div.

I want to be able to specify a REF of my section container and only animate from within that ref when the section is within the vewport.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
liquefliescommented, Nov 6, 2022

Hello, I have a particular issue with useScroll({container}) option. If you are on a child component and you want to track its scroll progress based on a container ref that it’s from the parent component, it will not trigger any value change. I tried passing the parent ref as a prop or getting the div with getElementById but it didn’t work. I have a list of Card that should parallax but are contained into a scrolling div instead window.

0reactions
olehkhalincommented, Dec 9, 2022

Hello, I have a particular issue with useScroll({container}) option. If you are on a child component and you want to track its scroll progress based on a container ref that it’s from the parent component, it will not trigger any value change. I tried passing the parent ref as a prop or getting the div with getElementById but it didn’t work. I have a list of Card that should parallax but are contained into a scrolling div instead window.

Have you managed to solve this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Animate elements based on container rather than page
When I want to animate a specific element in a specific section of a page, I have to create a really long-winded code...
Read more >
Animation Techniques for Adding and Removing Items From a ...
First, we need to make sure that the element we are dealing with is the .list-container element instead of the .list-item element. Due...
Read more >
CSS animations: elements in rows going outside of the ...
The problem I have is that they end up pushing against the left of the container instead of going outside it, and they...
Read more >
Using CSS animations - CSS: Cascading Style Sheets | MDN
CSS animations make it possible to animate transitions from one CSS style configuration to another. Animations consist of two components, ...
Read more >
Animation Using CSS Transforms - The Art of Web
Instead of :activate you can use a JavaScript onclick event to add a class to the element, and then CSS to apply the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found