Jump to content
Search Community

How to create triggers on panels that are absolutely positioned and scrolled with timeline?

xoxoxoxo test
Moderator Tag

Recommended Posts

I have a bunch of panels which are absolutely positioned. I'm trying to detect onLeave or onActive flag on these panels so I can change the color of an external element that is position fixed. For ex, menu..

 

How would this work on divs that aren't positioned normally? I have created a minimal demo where I'm iterating through the panels to create triggers and the markers are laid out inaccurately.

 

Basically I'm trying to change the color of the fix div when the panel leaves/enters the viewport.

See the Pen gOwMBvp?editors=1010 by nullhoooook (@nullhoooook) on CodePen

Link to comment
Share on other sites

Hey nullhook. Since you're controlling the position of your panels a normal ScrollTrigger applied to those panels likely doesn't match up with what you expect. Instead of using a ScrollTrigger for each panel to change your fixed item's color I recommend using the onStart or onComplete of your tween that moves your panels instead.

Link to comment
Share on other sites

well, the onComplete or onStart only fires when the panel touches the edge of the viewport. I need more granular control and ideally would like to fire events relative to fixed item's position. Is this possible with the event callbacks you mentioned above?

 

i.e, if the fixed item is placed 20px below the viewport's top my onComplete should fire when the panel's end is around 80-90%

 

Here's a minimal demo that changes rand color but it's edge to edge: 

See the Pen OJRRRpo?editors=0010 by nullhoooook (@nullhoooook) on CodePen

Link to comment
Share on other sites

6 hours ago, xoxoxoxo said:

ideally would like to fire events relative to fixed item's position

The fixed item is always fixed in the same position... So anything that's relative to the top of the viewport + the offset and height of the fixed item is relative to the fixed item. You could easily calculate that value yourself. 

const offset = nav.offsetTop + nav.offsetHeight;

 

With that being said, what makes it more difficult in your situation is that you are taking control over the positioning of each panel. So you need to either

  1. calculate where that offset is relative to where your custom control starts each panel's movement
  2. calculate what percent the offset is of the total animation for each panel. Then use that percentage to fire off the function once the animation's progress has passed the percentage.
  • Like 1
Link to comment
Share on other sites

41 minutes ago, xoxoxoxo said:

I was thinking to get percentage of each panel like this and act based on the percentage if that's what you mean by your second point above?

Yep. But you'll have to compare that progress value with the progress value of when you want to fire your function. To get that value, you'll need to divide the offset and height of your fixed item by the total distance of your ScrollTrigger.

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