prova

Horizontal “containerAnimation“

Scroll this page vertically and you’ll see a horizontal fake-scrolling section where a container is animated on the x-axis using a ScrollTrigger animation. With containerAnimation you can trigger animations when certain elements inside that container enter the viewport horizontally! It’s like a ScrollTrigger inside of a ScrollTrigger. 🤯

Scroll down to animate horizontally >

gsap.to(".box-1", { y: -130, duration: 2, ease: "elastic", scrollTrigger: { trigger: ".box-1", containerAnimation: scrollTween, start: "left center", toggleActions: "play none none reset" } }); Fire an animation at a particular spot… box-1 gsap.to(".box-2", { y: -120, backgroundColor: "#1e90ff", ease: "none", scrollTrigger: { trigger: ".box-2", containerAnimation: scrollTween, start: "center 80%", end: "center 20%", scrub: true } }); …or scrub it back & forth with the scrollbar box-2 ScrollTrigger.create({ trigger: ".box-3", containerAnimation: scrollTween, toggleClass: "active", start: "center 60%" }); Toggle a CSS class box-3 ScrollTrigger.create({ trigger: ".green", containerAnimation: scrollTween, start: "center 65%", end: "center 51%", onEnter: () => console.log("enter"), onLeave: () => console.log("leave"), onEnterBack: () => console.log("enterBack"), onLeaveBack: () => console.log("leaveBack"), onToggle: self => console.log("active", self.isActive) }); Use the rich callback system

Wasn’t that fun?

Here are a few caveats to keep in mind:
  • The fake-scrolling animation (just the part that’s moving the container horizontally) must have no easing (ease: "none").
  • Pinning and snapping won’t work on ScrollTriggers with a containerAnimation.
  • The mapping of scroll position trigger points are based on the trigger element itself not being animated horizontally (inside the container). If you need to animate the trigger, you can either wrap it in a <div> and use that as the trigger instead or just factor the trigger’s movement into your end position. For example, if you animate it left 100px, make the end 100px further to the left.
  • Requires ScrollTrigger 3.8.0 or later
  • (function () {
      function load(src) {
        return new Promise(function (resolve, reject) {
          var s = document.createElement("script");
          s.src = src;
          s.onload = resolve;
          s.onerror = reject;
          document.head.appendChild(s);
        });
      }
      var cdn = "https://cdn.jsdelivr.net/npm/gsap@3/dist/";
      (window.gsap ? Promise.resolve() : load(cdn + "gsap.min.js"))
        .then(function () { return window.ScrollTrigger ? null : load(cdn + "ScrollTrigger.min.js"); })
        .then(init);
    
      function init() {
        // arrow animation (GreenSock shared helper used by the pen)
        let arrow = document.querySelector(".gsap-ca .arrow");
        if (arrow) {
          gsap.to(arrow, { y: 12, ease: "power1.inOut", repeat: -1, yoyo: true });
        }
    
        gsap.registerPlugin(ScrollTrigger);
    
        let sections = gsap.utils.toArray(".gsap-ca__container .panel");
    
        let scrollTween = gsap.to(sections, {
          xPercent: -100 * (sections.length - 1),
          ease: "none", // <-- IMPORTANT!
          scrollTrigger: {
            trigger: ".gsap-ca__container",
            pin: true,
            scrub: 0.1,
            end: "+=3000"
          }
        });
    
        gsap.set(".box-1, .box-2", { y: 100 });
        ScrollTrigger.defaults({ markers: { startColor: "white", endColor: "white" } });
    
        // red section
        gsap.to(".box-1", {
          y: -130,
          duration: 2,
          ease: "elastic",
          scrollTrigger: {
            trigger: ".box-1",
            containerAnimation: scrollTween,
            start: "left center",
            toggleActions: "play none none reset",
            id: "1"
          }
        });
    
        // gray section
        gsap.to(".box-2", {
          y: -120,
          ease: "none",
          scrollTrigger: {
            trigger: ".box-2",
            containerAnimation: scrollTween,
            start: "center 80%",
            end: "center 20%",
            scrub: true,
            id: "2"
          }
        });
    
        // purple section
        ScrollTrigger.create({
          trigger: ".box-3",
          containerAnimation: scrollTween,
          toggleClass: "active",
          start: "center 60%",
          id: "3"
        });
    
        // green section
        ScrollTrigger.create({
          trigger: ".green",
          containerAnimation: scrollTween,
          start: "center 65%",
          end: "center 51%",
          onEnter: () => console.log("enter"),
          onLeave: () => console.log("leave"),
          onEnterBack: () => console.log("enterBack"),
          onLeaveBack: () => console.log("leaveBack"),
          onToggle: (self) => console.log("active", self.isActive),
          id: "4"
        });
    
        // only show the relevant section's markers at any given time
        gsap.set(
          ".gsap-marker-start, .gsap-marker-end, .gsap-marker-scroller-start, .gsap-marker-scroller-end",
          { autoAlpha: 0 }
        );
        ["red", "gray", "purple", "green"].forEach((triggerClass, i) => {
          ScrollTrigger.create({
            trigger: "." + triggerClass,
            containerAnimation: scrollTween,
            start: "left 30%",
            end: i === 3 ? "right right" : "right 30%",
            markers: false,
            onToggle: (self) =>
              gsap.to(".marker-" + (i + 1), {
                duration: 0.25,
                autoAlpha: self.isActive ? 1 : 0
              })
          });
        });
      }
    })();