Jump to content
Search Community

Nesting an animation trigger inside scrolltrigger element

BearAxe test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Context: I'm trying to create a site with pinned panels that trigger animations inside these panels when you scroll down. Seems simple enough.

 

Problem: When I repurpose code out of the docs to try and get the green box to move, something about the way i'm doing it isn't allowing the green box to rotate in towards the center of the viewport when I scroll down.

 

Attempts: I've tried changing around what selectors are used, but I left it in the state that i'm pretty sure is supposed to be correct. I think I may be having a fundamental misunderstanding of what something means, but I'm so new to this that I can't seem to figure out what that may be.

I did try looking through the gsap demos of scroll trigger to see what I could find, but they all seem to be more complex than what I'm trying to do. Any help would be greatly appreciated.

See the Pen ExREozq by skaulox (@skaulox) on CodePen

Link to comment
Share on other sites

  • Solution

Hi,

 

You didn't set up the scroller property in the ScrollTrigger configuration, so basically that instance is waiting for the window to scroll up/down which never happens. This seems to fix it:

function setupOneOffs() {
  
  let panel1 = gsap.timeline({
    // yes, we can add it to an entire timeline!
    scrollTrigger: {
      trigger: ".content1",
      pin: false,
      scroller: ".container2", // <- Add the scroller element here
      start: "top top",
      end: "+=50",
      snap: {
        snapTo: "labels",
      },
    },
  });

// add animations and labels to the timeline
panel1.addLabel("start")
  .from(".test1", {x: -500, scale: .3, rotation:45})
  .addLabel("color")
  .from(".test1", {backgroundColor: "#28a92b"})
  .addLabel("spin")
  .to(".test1", {rotation: 360, x: 0})
  .addLabel("end");
};

Happy Tweening!

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Works like a charm! Thank you so much!

 

If anyone else comes across this and wonders why my solution doesn't allow for the animation to replay on scrolling back up, like I initially did, you'll need to uncomment the scrub line to enable that behavior.

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