Jump to content
Search Community

Apply same scrolltrigger animation to multiple elements with same class

archimedo test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hello, I created a simple scrolltrigger animation that shows text when a card become visibile in the screen.

Since I have multiple similar cards on my page, I thought it would be good to apply the same code to all of them together.

I tried with a loop but it seems not to be working, can you help me understand what I'm missing?

Thanks!

See the Pen bGyNgag by aieio (@aieio) on CodePen

Link to comment
Share on other sites

22 hours ago, GreenSock said:

You had various syntax problems and logic problems in your code. Plus you weren't loading or registering ScrollTrigger. Is this what you're looking for?: 

 

 

 

Thank you so much for your help!

I was noticing that when I scroll up again the animation sometimes restarts. is there a way where the animation is executed only once?

Link to comment
Share on other sites

17 hours ago, archimedo said:

I was noticing that when I scroll up again the animation sometimes restarts. is there a way where the animation is executed only once?

You mean if you scroll back up and down again?

 

In that case just use the once config option:
https://gsap.com/docs/v3/Plugins/ScrollTrigger/?page=1#once

 

ScrollTrigger.batch(".section", {
  onEnter: (batch) => {
    let targets = [];
    batch.forEach(el => {
      targets.push(...Array.from(el.querySelectorAll('h1, p')))
    });
    gsap.from(targets, {
      y: "100%",
      opacity: 0,
      stagger: 0.3,
      duration: 1.2,
      delay: 0.6,
      ease: "power4.out",
      stagger: {
        each: 0.3,
        from: "start",
        ease: "none"
      }
    });
  },
  start: "top bottom",
  once: true,
});

Hopefully this helps.

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

21 minutes ago, Rodrigo said:

You mean if you scroll back up and down again?

 

In that case just use the once config option:
https://gsap.com/docs/v3/Plugins/ScrollTrigger/?page=1#once

 

ScrollTrigger.batch(".section", {
  onEnter: (batch) => {
    let targets = [];
    batch.forEach(el => {
      targets.push(...Array.from(el.querySelectorAll('h1, p')))
    });
    gsap.from(targets, {
      y: "100%",
      opacity: 0,
      stagger: 0.3,
      duration: 1.2,
      delay: 0.6,
      ease: "power4.out",
      stagger: {
        each: 0.3,
        from: "start",
        ease: "none"
      }
    });
  },
  start: "top bottom",
  once: true,
});

Hopefully this helps.

Happy Tweening!

Yes that's exactly what I was looking for!

it seems to work great! the only problem is that if I reload the page and the scrollbar is down already, the animation won't start immediately but it will wait for all the other elements on top to finish. 

Is there a way to make the animation start immediately and not make it dependent on the other sections?

Link to comment
Share on other sites

  • Solution

Hi,

 

Just set the maximum number of elements for each batch to be 1 with :

https://gsap.com/docs/v3/Plugins/ScrollTrigger/static.batch()#configuration

 

Something like this:

ScrollTrigger.batch(".section", {
  onEnter: (batch) => {
    let targets = [];
    batch.forEach(el => {
      targets.push(...Array.from(el.querySelectorAll('h1, p')))
    });
    gsap.from(targets, {
      y: "100%",
      opacity: 0,
      stagger: 0.3,
      duration: 1.2,
      delay: 0.6,
      ease: "power4.out",
      stagger: {
        each: 0.3,
        from: "start",
        ease: "none"
      }
    });
  },
  batchMax: 1,
  start: "top bottom"
});

Hopefully this helps.

Happy Tweening!

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...