Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                video,
img {
  position: absolute;
  left: 0;
  top: 0;

  width: 100%;
  height: 100%;
  object-fit: cover;
  will-change: transform;
}

.slides {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

.slide {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100vh;
  clip-path: polygon(0 100%, 100% 100%, 100% 100%, 0 100%);

  &:first-child {
    clip-path: polygon(0% 100%, 100% 100%, 100% 0%, 0% 0%);
  }

  a {
    position: relative;
    display: grid;
    height: 100vh;
    text-decoration: none;
    overflow: hidden;
  }
}

.outer,
.inner {
  width: 100%;
  height: 100%;
  overflow-y: hidden;
}

.container {
  display: grid;
  position: relative;
  height: 100vh;
  background-color: orange;
}

.slides,
.nav,
.bg {
  grid-area: 1 / 1;
}

.bg {
  position: relative;
  overflow: hidden;
  width: 100%;
  height: 100%;
  will-change: transform;
  perspective: 1px;
}

.nav {
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-self: center;
  justify-self: flex-start;
  list-style-type: none;

  > li {
    &:first-child {
      padding-left: 24px;

      @media (min-width: 799px) {
        padding-left: 64px;
      }
    }

    button {
      display: inline-flex;
      align-items: center;
      cursor: pointer;
    }

    svg {
      width: 35px;
      margin-right: 12px;
      color: #fff;
      display: none;
    }

    h2 {
      color: blue;
      text-align: start;
    }

    &.is-active {
      svg {
        display: block;
      }
    }
  }
}

.line {
  width: 100%;
  height: 10px;
  position: absolute;
  top: 0;
  z-index: 1;
  display: inline-block;
  background-color: chocolate;
}

.menu {
  border: 1px solid red;
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  z-index: 1000;

  ul {
    display: flex;
    gap: 2em;
  }
}

              
            
!

JS

              
                const { useEffect, useState, useRef } = React;

gsap.registerPlugin(ScrollToPlugin, ScrollTrigger);

const sliderArr = [
  {
    id: 1,
    href: "/",
    src:
      "https://images.unsplash.com/photo-1526409049865-5b0b188372f1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1632&q=50",
    navTitle: "Slide 1"
  },
  {
    id: 2,
    href: "/",
    src:
      "https://images.unsplash.com/photo-1425913397330-cf8af2ff40a1?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1074&q=50",
    navTitle: "Slide 2"
  },
  {
    id: 3,
    href: "/",
    src:
      "https://images.unsplash.com/photo-1551986782-d0169b3f8fa7?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=688&q=50",
    navTitle: "Slide 3"
  },
  {
    id: 4,
    href: "/",
    src:
      "https://images.unsplash.com/photo-1608478497783-158b45d36042?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=50",
    navTitle: "Slide 4"
  }
];

function App() {
  const containerRef = useRef(null);
  const slidesWrapperRef = useRef(null);
  const slides = useRef([]);
  const navItems = useRef([]);
  const bg = useRef([]);
  const lineRef = useRef([]);
  const slidesTl = useRef();
  const duration = useRef(1);
  const [ctx] = useState(gsap.context(() => {}, containerRef));
  const [activeNavItem, setActiveNavItem] = useState(0);

  useEffect(() => {
    ctx.add(() => {
      // const slides = gsap.utils.toArray(".slide");

      let breakPoint = 800;
      let mm = gsap.matchMedia();

      slidesTl.current = gsap.timeline({
        id: "hero-animation",
        defaults: {
          ease: "none",
          duration: 1
        },
        scrollTrigger: {
          trigger: containerRef.current,
          pin: true, // locked the slidesWrapperRef div with position fixed
          start: "top top", // init slides at the top of the viewport
          end: `+=${sliderArr.length * 100}%`, // intead of using "bottom top" move end marker to the end of 3rd slide to slow down scroll animation

          scrub: true,
          invalidateOnRefresh: true, // to make it responsive
          markers: true
        }
      });

      // each slide
      slides.current.forEach((slide, index) => {
        // exclude the first slide
        // since we want to start animation from the second slide (index 1)
        if (index > 0) {
          if (!slidesTl.current) return;
          const position = duration.current / 2; // add active class at the center of the viewport

          console.log(bg.current[index - 1]);

          mm.add("(min-width: 799px)", () => {
            // this setup code only runs when viewport is at least 800px wide
            slidesTl.current
              .to(navItems.current[index - 1], { paddingLeft: 0 }, "<")
              .to(navItems.current[index], { paddingLeft: 50 }, "<");
          });

          mm.add("(max-width: 800px)", () => {
            // this setup code only runs when viewport is at least 800px wide
            slidesTl.current
              .to(navItems.current[index - 1], { paddingLeft: 0 })
              .to(navItems.current[index], { paddingLeft: 100 });
          });

          slidesTl.current
            .to(slide, {
              clipPath: "polygon(0% 100%, 100% 100%, 100% 0%, 0% 0%)"
            })
            .fromTo(
              bg.current[index - 1].firstElementChild,
              {
                y: 0
              },
              {
                y: () => -slide.offsetHeight * 0.3
              },
              "<"
            )
            .fromTo(
              bg.current[index].firstElementChild,
              {
                y: () => slide.offsetHeight * 0.3
              },
              {
                y: () => 0
              },
              "<"
            )
            .call(
              () => {
                const st = slidesTl.current?.scrollTrigger;
                //console.log(slidesTl.current?.scrollTrigger.progress);
                if (st && st.direction !== undefined) {
                  const _d = st.direction;
                  setActiveNavItem(index + (_d > 0 ? 0 : _d));
                }
              },
              undefined,
              `-=${String(position)}`
            )
            .fromTo(
              lineRef.current[index],
              {
                value: 0
              },
              {
                value: 100,
                ease: "none"
              },
              "<"
            );
        }

        // ScrollTrigger.create({
        //   trigger: slide,
        //   start: "top center",
        //   end: "bottom center",
        //   markers: true
        // });
      });
    });

    return () => {
      ctx.revert();
    };
  }, []);

  const slideToSection = (index) => {
    const navItemsCount = navItems.current.length - 1;
    const st = slidesTl.current?.scrollTrigger;

    if (st && st.start !== undefined && st.end !== undefined) {
      // calculate the y-coordinate based on the scrollTrigger
      const y = st.start + (st.end - st.start) * ((1 / navItemsCount) * index);
      console.log({ navItemsCount, index });

      gsap.to(window, {
        scrollTo: { y },
        duration: duration.current
      });
    }
  };

  return (
    <>
      <header className="menu">
        <div className="inner-menu">
          <ul>
            <li>item 1</li>
            <li>item 2</li>
          </ul>
        </div>
      </header>
      <section className="container" ref={containerRef}>
        <div className="slides" ref={slidesWrapperRef}>
          {sliderArr.map((slide, index) => {
            return (
              <div
                className={`slide`}
                id={`slide-${index}`}
                aria-hidden={index === activeNavItem ? false : true}
                ref={(element) => {
                  // check if the element exists and if it's not already present
                  if (element && !slides.current.includes(element)) {
                    slides.current.push(element);
                  }
                }}
              >
                <div className="outer">
                  <div className="inner">
                    <progress
                      className="line"
                      max="100"
                      value="0"
                      ref={(element) => {
                        if (element && !lineRef.current.includes(element)) {
                          lineRef.current.push(element);
                        }
                      }}
                    ></progress>
                    <a href={slide.href}>
                      <div
                        className="bg"
                        ref={(element) => {
                          if (element && !bg.current.includes(element)) {
                            bg.current.push(element);
                          }
                        }}
                      >
                        <img src={slide.src} alt="" />
                      </div>
                    </a>
                  </div>
                </div>
              </div>
            );
          })}
        </div>
        <ul className="nav">
          {sliderArr.map(({ navTitle, id }, idx) => {
            return (
              <li
                key={id}
                ref={(element) => {
                  if (element && !navItems.current.includes(element)) {
                    navItems.current.push(element);
                  }
                }}
                className={idx === activeNavItem ? "is-active" : ""}
              >
                <button
                  onClick={() => slideToSection(idx)}
                  data-href={`#slide-${idx}`}
                  type="button"
                >
                  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
                    <path d="M165.013 288.946h75.034c6.953 0 12.609 5.656 12.609 12.608v26.424c0 7.065 3.659 9.585 7.082 9.585 2.106 0 4.451-.936 6.78-2.702l90.964-69.014c3.416-2.589 5.297-6.087 5.297-9.844 0-3.762-1.881-7.259-5.297-9.849l-90.964-69.014c-2.329-1.766-4.674-2.702-6.78-2.702-3.424 0-7.082 2.519-7.082 9.584v26.425c0 6.952-5.656 12.608-12.609 12.608h-75.034c-8.707 0-15.79 7.085-15.79 15.788v34.313c0 8.706 7.082 15.79 15.79 15.79z" />
                    <path d="M256 0C114.842 0 .002 114.84.002 256S114.842 512 256 512s255.998-114.84 255.998-256S397.158 0 256 0zm0 66.785c104.334 0 189.216 84.879 189.216 189.215S360.334 445.215 256 445.215 66.783 360.336 66.783 256 151.667 66.785 256 66.785z" />
                  </svg>
                  <h2>{navTitle}</h2>
                </button>
              </li>
            );
          })}
        </ul>
      </section>
      <section className="container">
        <div className="inner">
          <h1>HELLO!!!</h1>
        </div>
      </section>
      <section className="container">
        <div className="inner">
          <h1>HELLO!!! AGAIN</h1>
        </div>
      </section>
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);

// console.clear();

// gsap.registerPlugin(ScrollTrigger);

// const slides = gsap.utils.toArray(".slide");

// const slidesTl = gsap.timeline({
//   defaults: {
//     ease: "none"
//   },
//   scrollTrigger: {
//     trigger: ".container",
//     pin: true,
//     start: "top top",
//     end: "bottom top",
//     scrub: true,
//     markers: true
//   }
// });

// slides.forEach((slide, i) => {
//   console.log("fired", slide, i);

//   slidesTl.to(slide, {
//     clipPath: "polygon(0% 100%, 100% 100%, 100% 0%, 0% 0%)"
//   });
// });

              
            
!
999px

Console