javascriptgsapscrolltrigger

gsap scrollTrigger is not being pinned during the animation


I'm really new to GSAP and my way of doing might be wrong.

I've created a code pen : https://codepen.io/Emma-miu/pen/dyBYKYe

GSAP settings:

gsap.to(".before", {
            scrollTrigger: {
                trigger: ".flex",
                start: "top top",
                end: () => "+=" + document.querySelector(".before").offsetWidth,
                scrub: "true",
                pin: "true",
                toggleAction: "play none none reverse"
            },
            x: '-100%',
            duration: 2
});

gsap.to(".after", {
            scrollTrigger: {
                trigger: ".flex",
                start: "top top",
                end: () => "+=" + document.querySelector(".after").offsetWidth,
                scrub: "true",
                pin: "true",
                toggleAction: "play none none reverse"
            },
            x: '100%',
            duration: 2
});

In my example, i have gsap-container div, going from white to showing the background image. I would like to 'pin' the container for the duration of the animation.

So i will only see the white to background div until the background is fully showing then I will be able to continue scrolling down.

I thought "pin: true" settings will achieve that but i can still scroll down to red box

Can somebody tell me what i'm doing wrong here ?


Solution

  • You don't need pin two ScrollTriggers for that...

    You need to tweak a little bit end value and synchronized transition, create value statically. Also, keep in mind that you pinned the same container twice.

    gsap.registerPlugin(ScrollTrigger);
    
    gsap.to(".before", {
      scrollTrigger: {
        trigger: ".gsap-container",
        start: "top top",
        end: "+=100%",
        scrub: true,
        pin: true,
        toggleActions: "play none none reverse"
      },
      x: '-100%',
      duration: 2
    });
    
    gsap.to(".after", {
      scrollTrigger: {
        trigger: ".gsap-container",
        start: "top top",
        end: "+=100%",
        scrub: true,
        pin: false, // No needing
        toggleActions: "play none none reverse"
      },
      x: '100%',
      duration: 2
    });
    .big-space {
      width: 100%;
      height: 100vh;
      background-color: red;
      text-align: center;
      font-size: 30px;
    }
    
    .blue {
      background-color: teal;
    }
    
    .flex {
      display: flex;
      width: 100%;
      height: 100vh;
      background-image: url("https://picsum.photos/200");
      background-position: center;
      background-size: cover;
    }
    
    .gsap-container {
      position: relative;
      height: 100vh;
    }
    
    .text-container {
      position: absolute;
      top: 50%;
      left: 50%;
      text-align: center;
    }
    
    h2, p {
      color: white;
      mix-blend-mode: difference;
    }
    
    .before {
      width: 50%;
      height: 100vh;
      background-color: white;
    }
    
    .after {
      width: 50%;
      height: 100vh;
      background-color: white;
    }
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js"></script>
    
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/ScrollTrigger.min.js"></script>
    
    <div class="big-space">scroll down</div>
    <div class="gsap-container">
      <div class="flex">
        <div class="before"></div>
        <div class="after"></div>
      </div>
      <div class="text-container">
        <h2>Hello</h2>
        <p>this is some text</p>
      </div>
    </div>
    <div class="big-space"></div>
    <div class="big-space blue"></div>