Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
jh3y

Pinning a Contained ScrollTrigger

Go to solution Solved by GreenSock,

Recommended Posts

Howdy Y'all! ๐Ÿ‘‹

ย 

This may be the way to do it or there may be a cleaner way ๐Ÿ˜…

ย 

But, if you want to `pin` something inside a `scroller` element, is this the way to do it?

ย 

const TRIGGER = ScrollTrigger.create({
  horizontal: false,
  trigger: '.scroller',
  pin: '.panel',
  end: '+=2000',                          
  scroller: '.scroller',
  onUpdate: self => {
    // Doing anything here for hypotheticals ๐Ÿ˜…
    gsap.set('.panel', {
      rotate: self.progress * 360,
    })
  },
})

The "trick" I'm using is that if you use CSS `transform` on the container, that will scope `fixed` to the container. Or is there another API way of doing this?

See the Pen eYgJJQg by jh3y (@jh3y) on CodePen

Link to comment
Share on other sites

15 minutes ago, mikel said:

Hey @jh3y,

ย 

Could this be an alternative?

ย 

ย 

ย 

ย 

Happy scrolling ...

Mikel

ย 

Hey ๐Ÿ‘‹

ย 

Yeah, that's almost the same approach which confirms it to me ๐Ÿ‘

`position: relative` has the same effect as applying a `transform` on the container for `fixed` children.

ย 

That demo you provided works with the `padding` removed and `height: 100%` set on the `#container`. In its current form, the green box will scroll up towards the end of the container ๐Ÿ‘

ย 

But, thank you, this confirmed my thinking in how to approach it ๐Ÿ™ I have a sneaky suspicion there will be some requests to contain the scrolling demo I've been working on into portable containers and wanted to make sure I had the right idea for doing this before going forwards ๐Ÿ‘

ย 

Thanks again!

ย 

Jhey

Link to comment
Share on other sites

@jh3yย it sounds like you got it all figured out, but I was curious - is there some reason you need position: fixed on the element?ย 

ย 

By default, ScrollTrigger pins things that are at the root (body) level with position: fixed because that's essential to avoid synchronizing issues due to the fact that modern browsers handle scrolling that root element on a different thread. But nested scrolling is handled differently, so ScrollTrigger uses transforms to simulate the pinning. In your case, that acts strangely [if the parent has no transforms] because your pinned element is position: fixed. You could set pinType: "fixed", but the position when unpinning is negatively affected.ย 

Link to comment
Share on other sites

Hey @GreenSockย ๐Ÿ‘‹

ย 

The use case was about being able to pin something in a container that can be moved around. For when there'sย nested scrolling and I want to pin something in the container with nested scroll.

ย 

I'm relying on CSS here where elements fix relative to the container if it has a transform or position relative. The idea is that it wouldn't be unpinned in the container kinda like the infinite scroller.

ย 

It was more a case of wondering how this might be done. For example, I anticipate the content scroller might want to be put in its own container and moved around without relying on body scroll. Didn't know if there was an appropriate approach. But, the pen I posted seems to work for my needs ๐Ÿ‘

  • Like 1
Link to comment
Share on other sites

  • Solution
17 minutes ago, jh3y said:

The use case was about being able to pin something in a container that can be moved around. For when there'sย nested scrolling and I want to pin something in the container with nested scroll.

Yeah, I figured - I'm just not clear about why you needed to set position: fixed in the CSS for the thing getting pinned. Why not just position: absolute | relative and let ScrollTrigger do the pinning to make it appear fixed? I'm not saying you SHOULD or that there's anything "wrong" with what you were doing. It's more of a curiosity for me, that's all.ย 

ย 

Glad you've got it all working the way you want.ย ๐Ÿ‘

Link to comment
Share on other sites

No worries. That was kinda what I was looking to find out with opening this thread ๐Ÿ˜ "Was I missing something straightforward?". I haven't really played with nested scroll and ScrollTrigger ๐Ÿ˜

ย 

I don't think I knew thatย there were two different pin styles. I think I assumed that `position: fixed` was already being used, not that it was being kept in place by `transform` which I should've noticed really ๐Ÿ˜…

ย 

As you say, `position: absolute` will do the trick and means I don't need to set anything on the child element ๐Ÿ‘

ย 

Thanks!ย 

Link to comment
Share on other sites

22 minutes ago, jh3y said:

I don't think I knew thatย there were two different pin styles. I think I assumed that `position: fixed` was already being used, not that it was being kept in place by `transform` which I should've noticed really ๐Ÿ˜…

ย 

Yeah, it all has to do with the pesky ways that browsers handle things in different threads but only for the main page scrolling.ย 

ย 

Either solution should work, so use whatever fits best into your scenario.ย 

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