Jump to content
Search Community

Horizontal Scroll w/ ScrollTrigger - Change Background Color Scrolling Forwards and Backwards

Leonardo da Costa test
Moderator Tag

Recommended Posts

Hi team! I'm trying to build a horizontal scroll section where the background color fade to a different one whenever the next sections comes into view on scroll. I was able to accomplish this when scrolling forwards during the first time through, however, when scrolling backwards the background stays the same as from the last section. Thank you in advanced for your time, let me know if this is something that is doable. Thanks!

See the Pen qBKjJPG by costaleonardo (@costaleonardo) on CodePen

Link to comment
Share on other sites

Hi @Leonardo da Costa and welcome to the GreenSock forums!

 

Just use the toggleActions config in order to reverse the animation when the section enters back into the viewport. This seems to work the way you want:

ScrollTrigger.create({
  trigger: sct,
  start: () => 'top top-=' + (sct.offsetLeft - window.innerWidth/2) * (maxWidth / (maxWidth - window.innerWidth)),
  // end: () => '+=' + sct.offsetWidth * (maxWidth / (maxWidth - window.innerWidth)),
  toggleClass: { targets: sct, className: "active"},
  animation: colorChange,
  toggleActions: "play none reverse none",
});

From the docs:

Quote

String - Determines how the linked animation is controlled at the 4 distinct toggle places - onEnter, onLeave, onEnterBack, and onLeaveBack, in that order. The default is play none none none. So toggleActions: "play pause resume reset" will play the animation when entering, pause it when leaving, resume it when entering again backwards, and reset (rewind back to the beginning) when scrolling all the way back past the beginning. You can use any of the following keywords for each action: "play", "pause", "resume", "reset", "restart", "complete", "reverse", and "none".

You can find out more about it here:

https://greensock.com/docs/v3/Plugins/ScrollTrigger

 

Let us know if you have more questions.

 

Happy Tweening!

Link to comment
Share on other sites

Hi,

 

The issue seems to be on the start values in the sections ScrollTrigger instances.

 

This seems to be working closer to what you want:

See the Pen XWYeagd by GreenSock (@GreenSock) on CodePen

 

Also I removed a lot of your logic in the loop event since it seemed quite unnecessary and it works better. You still might have to tinker a bit with the start and end values, but hopefully this is a good starting point.

 

Happy Tweening!

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