Jump to content
Search Community

How to use the same trigger for two divs?

rusakovsb test
Moderator Tag

Go to solution Solved by akapowl,

Recommended Posts

Hello! I have two blocks with same classes .container. Inside each .container I want to have two animated divs: .box1 and .box2. Animation for .box1 and .box2 must start when parent .container is in viewport. How to use the same trigger for two divs?

 

gsap.utils.toArray(".div1").forEach(box1 => {
        gsap.to(box1, {
            ease: "none",
            transform: "translateY(-5vw)", 
            scrollTrigger: {
                trigger: container,
                start: 'top bottom',
                end: 'top top',
                scrub: 0.1
            }
        });
    });

    gsap.utils.toArray(".div2").forEach(box2 => {
        gsap.to(box2, {
            ease: "none",
            transform: "translateY(5vw)",         
            scrollTrigger: {
                trigger: container,
                start: 'top bottom',
                end: 'top top',
                scrub: 0.1
            }
        });
    });

 

Link to comment
Share on other sites

  • rusakovsb changed the title to How to use the same trigger for two divs?
  • Solution

 

Hello @rusakovsb and welcome to the forums!

 

You could loop over all your boxes instead and apply different values to properties by using ternary operators/if-else or only if statements e.g.

 

Oh, almost forgot: it is not recommended to tween on your trigger element like this when it will change the dimensions or positioning to where it was initially, so best just wrap what you are going to tween on and use that wrap as the trigger.

 

And you don't need to use transform: "translateY(5vw)" , you can just say y: "5vw" instead .

 

Btw, minimal demos make it easier to visualize things ;) 

 

See the Pen BaJOYPM by akapowl (@akapowl) on CodePen

 

  • Like 3
Link to comment
Share on other sites

1 hour ago, akapowl said:

 

Hello @rusakovsb and welcome to the forums!

 

You could loop over all your boxes instead and apply different values to properties by using ternary operators/if-else or only if statements e.g.

 

Oh, almost forgot: it is not recommended to tween on your trigger element like this when it will change the dimensions or positioning to where it was initially, so best just wrap what you are going to tween on and use that wrap as the trigger.

 

And you don't need to use transform: "translateY(5vw)" , you can just say y: "5vw" instead .

 

Btw, minimal demos make it easier to visualize things ;) 

 

 

 

 

Thanks. It helped me find a solution.

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