Jump to content
Search Community

Pin object within pinned object

Sam Smyth test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hi - I'm very new to GSAP, so I may be going about this issue incorrectly.

 

Here's my goal:

  • When a user scrolls down and the "Test" text meets the middle of the viewport, this "Test" text should stay pinned in the middle of the viewport until it reaches the middle of the ".second" element
  • When the text reaches the middle of the ".second" element, I want the text to stay in the middle of the ".second" element as the ".second" element is pinned to the viewport. 

 

The codepen is as close as I can get to what I want, but there are a few issues:

  • The text jumps down to the middle of the ".second" element and oddly moves left when it should get pinned to the middle of the viewport and scroll nicely.
  •  I need to be able to have the text start a little bit down from the top of the ".second" element. Similar to how you would add some padding if there was no animation. 

 

If you comment out the first half of my JS, you'll see that it works correctly besides the ".second" element not getting pinned (or having any padding) when it reaches the top of the viewport. 

 

Using this on my project, I'm running an animation timeline in the center of the ".second" element, but I don't want a large amount of empty space in the top half of this element as the user scrolls down until the ".second" element meets the top of the viewport (since the element is 100vh, there's a lot of empty space) - I want the user to see the text fairly near the element above so they don't think it's the end of the page.

 

Hopefully this makes sense! Thanks so much.

See the Pen VwbNYjx by ssmyth93 (@ssmyth93) on CodePen

Link to comment
Share on other sites

Hmm, that's close to what I'm looking for, but it still shoots down the page as you scroll rather than staying pinned to the center of the viewport. It's almost like it starts animating a little bit late and has to catch up.

 

I'm looking to have it work just like Jack's example but have the "Test" text start a little bit down into the element rather than at the top. Any way I can do that?

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