Jump to content
Search Community

ScrollTrigger - trying to animate when element leaves the screen

theodieu test
Moderator Tag

Recommended Posts

Hello,

I'm new with gsap/ScrollTrigger. I'm trying to animate the sections of a page when they enter the screen, and then when they leave the screen.

It works well when they enter, but I don't get them to work when they leave... See the codepen it will speak by itself.

What am I doing wrong?

 

And another little detail : the section is "jumping" when the first ScrollTrigger reaches the end of the pin. Why?

 

Thank you!

See the Pen pobJoyq by theodieu (@theodieu) on CodePen

Link to comment
Share on other sites

There's a couple of main issues at play here:

  1. Your end position for the first ScrollTrigger is after the start of your second ScrollTrigger. Thus they conflict and there's a jump. 
  2. Even if you fix 1, the code will still not work because your first ScrollTrigger's start position is before a scroll position of 0. To fix that, simply use a start of "top top" or add some space before the first ScrollTrigger. I did that with padding in the demo below but you could do it in a whole lot of ways.

Here's how I'd set it up. In general it's a good idea to use a container element if you're going to use ScrollTriggers and y translations or transforms:

See the Pen GRqpgVa?editors=0010 by GreenSock (@GreenSock) on CodePen

 

Side notes: 

  • Avoid using top/left. They aren't performant to animate. Use y instead (which makes use of transforms).
  • You don't really need jQuery for this sort of thing.
  • Like 2
Link to comment
Share on other sites

Great! Thank you so much for the detailed explanation, and the extra tips!

It all works well now.

If I understand well the issue 1 you mentioned (please correct me if I'm wrong) : animations of the same element should not overlap.

If for any reason I want them to overlap, would using timeline solve the issue?

Link to comment
Share on other sites

4 hours ago, theodieu said:

animations of the same element should not overlap.

If for any reason I want them to overlap, would using timeline solve the issue?

Animations that affect the same properties of the same elements should not overlap. They are in direct conflict. Similarly ScrollTriggers who have tween that affect the same properties of the same elements should not overlap. 

 

No, using a timeline would not fix the issue (if you're saying that the overlap still exists) because it's a logical issue: things cannot be in two places at the same time. 

  • Like 1
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...