Jump to content
Search Community

animate snap-scrolling items

Mando test
Moderator Tag

Recommended Posts

hi guys I'm trying to animate some cards in my project, the cards are within a scrollable element, in a few fords I want the card that is in the center of the viewport to fade out and the rest to remain at their current state, but whenever I scroll they fade out all at once, any help?

I'll leave the code here

function isVisible (ele) {
    const { top, bottom } = ele.getBoundingClientRect();
    const vHeight = (window.innerHeight || document.documentElement.clientHeight);
    return (
        (top > 0 || bottom > 0) &&
        top < vHeight
    );
}

projectWrapper.addEventListener('scroll', () => {
    [...projectCards].forEach((card, i) => {
        if (isVisible(card)){
            gsap.to(`.project:nth-child(${i})`,{opacity: 0, duration: 2})
        }
    })
})

 

See the Pen dyeMaWW by STR4NG3R (@STR4NG3R) on CodePen

Link to comment
Share on other sites

Yeah, the way you're doing that is extremely inefficient (performance-wise) because you're calling getBoundingClientRect() on every element on every scroll event. It's far more efficient (and simpler) to use ScrollTrigger. Plus your code was looking only at the vertical values but you're scrolling horizontally. 

 

Here's a quick fork that shows using ScrollTrigger: 

See the Pen YzLqbjY?editors=1010 by GreenSock (@GreenSock) on CodePen

 

Good luck! 👍

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...