Jump to content
Search Community

GSAP scrollTrigger animation

RyanReese test
Moderator Tag

Go to solution Solved by RyanReese,

Recommended Posts

$(".seek").each(function() {
  const _ = $(this);
  const fullHeight = _.find(".image-hero").height();
  const textHeight = _.find(".image-hero > header").height();
  gsap.to(this.querySelector(".image-hero > header"), {
    yPercent: (Math.round((fullHeight - textHeight) / 2)) / textHeight * -100,
    ease: "none",
    scrollTrigger: {
      trigger: this.querySelector(".image-hero"),
      scrub: true,
      onUpdate(e) {
        // console.log(e);
        // console.log(e.animation.ratio);
        // console.log(e.animation.vars.yPercent);
        // console.log(e.progress * 100 + "%");
        // console.log(HOME.getTransform($(".seek .image-hero > header")[0]).yValue);
        // console.log("--");
      }
    },
  });
});

So to explain this code, .seek is just a parent div. Nothing special going on there. .image-hero is a child div of .seek with 100vh. The > header is at the bottom of this element, and it needs to parallax scroll up when you get to the .seek container, and stop at the center of the page. This math I have going on is a fancy way of telling GSAP to yPercent the >header enough to where it's at the bottom of the page at the end of the animation. The problem is (I think) that GSAP gets to the final percent after the panel is fully off the screen. I'd like the (in this case) -199% yPercent value to be completed when the TOP of .seek is at teh top of the window. So the animation will START when .seek top of the element hits the bottom of the viewport. 

Not done yet, but for other design reasons, I cannot use transform for the yPercent. I will be converting that in the onUpdate() to a negative top margin. Is there a native way to convert yPercent to use a top margin? And I guess while I'm here, is there a way to more easily tell GSAP to stop >header at the middle of the screen instead of my convoluted math? I looked through the docs but nothing stood out.

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

Please don't include your whole project - just some simple colored <div> elements that show the general concept is best. The simpler the better! 

Here's a starter CodePen that loads all the plugins so you can just click "fork" at the bottom right and edit however you please: 

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

If you're using something like React/Next/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

scrollTrigger: {
          trigger: this.querySelector(".image-hero"),
          scrub: true,
          start: "top bottom",
          end: "top top",
          onUpdate(e) {
            // console.log(e);
            // console.log(e.animation.ratio);
            // console.log(e.animation.vars.yPercent);
            // console.log(e.progress * 100 + "%");
            // console.log(HOME.getTransform($(".seek .image-hero > header")[0]).yValue);
            // console.log("--");
          }
        },

I think adding these "start/end" made it start/stop at the proper times. Now I just need this somehow converted to a negatiev top margin. I'll begin my onUpdate work to convert it but wondering if you experts know of a better way.

Finally, onComplete, I'll add that "zoom" class.

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