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

              
                <section class="panel">
  Content
</section>

<section class="panel">
  Content
</section>

<section class="horizontal-scroll--1">
    <div class="horizontal-inner--1">
      
        <div class="slide one">
          <div class="caption">
            <h2>Slide One</h2>
          </div>
          <div class="bar"></div>
          <div class="custom-animation"></div>
        </div>
      
        <div class="slide two">
          <div class="caption">
            <h2>Slide Two</h2>
          </div>
          <div class="bar"></div>
          <div class="line"></div>
        </div>
      
        <div class="slide three">
          <div class="bar"></div>
          <div class="caption">
            <h2>Slide Three</h2>
            <p>I want this slide panel to pin here for a limited time, then the next slide to overlap on top of me.</p>
          </div>
          <div class="custom-animation"></div>
        </div>
      
      <div class="slide four">
        <div class="bar"></div>
          <div class="caption">
            <h2>Slide Four</h2>
            <p>I want this slide panel to overlap over the previous one (to the left).</p>
          </div>
       </div>

    </div>
</section>

<section class="panel">
  Content
</section>

<section class="horizontal-scroll--2">
    <div class="horizontal-inner--2">
      
        <div class="slide one">
          <div class="bar"></div>
        </div>
      
        <div class="slide two">
          <div class="bar"></div>
          <div class="line"></div>
        </div>
      
        <div class="slide three">
          <div class="bar"></div>
        </div>
      
      <div class="slide four">
        <div class="bar"></div>
      </div>

    </div>
</section>

<section class="panel">
  Content
</section>

<section class="horizontal-scroll--3">
    <div class="horizontal-inner--3">
      
        <div class="slide one">
          <div class="bar"></div>
        </div>
      
        <div class="slide two">
          <div class="bar"></div>
          <div class="line"></div>
        </div>
      
        <div class="slide three">
          <div class="bar"></div>
        </div>
      
      <div class="slide four">
        <div class="bar"></div>
      </div>

    </div>
</section>

<section class="panel">
  Content
</section>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  margin: 0;
  overflow-x: hidden;
  color: #000000;
}

.caption { 
  max-width: 80%;
}

.panel {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: sans-serif;
  font-size: 6vmin;
}

.custom-animation {
  position: relative;
  background: red;
  left: 10vw;
  height: 50vh;
  width: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: sans-serif;
  font-size: 6vmin;
}

.horizontal-scroll--1,
.horizontal-scroll--2,
.horizontal-scroll--3 {
  min-height: 100vh;
}

.horizontal-inner--1,
.horizontal-inner--2,
.horizontal-inner--3 {
  width: 300%;
  height: 100%;
  display: flex;
  flex-wrap: nowrap;
}

.horizontal-inner--1 .slide,
.horizontal-inner--2 .slide,
.horizontal-inner--3 .slide, {
  position: relative;
  width: 100vw;
  flex: 0 0 100vw;
  border: solid 2px red;
}

.horizontal-inner--1 .slide .bar,
.horizontal-inner--2 .slide .bar,
.horizontal-inner--3 .slide .bar {
  position: absolute;
  left: 25vw; 
  top: calc(50vh - 5px);
  width: 50vw;
  height: 10px;
  background: gainsboro;
  transform-origin: left center;
}





.horizontal-inner--1 .slide.one,
.horizontal-inner--2 .slide.one,
.horizontal-inner--3 .slide.one {
  flex: 0 0 50vw;
  width: 50vw;
}

.horizontal-inner--1 .slide.one .bar,
.horizontal-inner--2 .slide.one .bar,
.horizontal-inner--3 .slide.one .bar {
  width: 25vw;
  left: 12.5vw; 
}



.horizontal-inner--1 .slide.two,
.horizontal-inner--2 .slide.two,
.horizontal-inner--3 .slide.two {
  flex: 0 0 150vw;
  width: 150vw;
}

.horizontal-inner--1 .slide.two .bar,
.horizontal-inner--2 .slide.two .bar,
.horizontal-inner--3 .slide.two .bar {
  width: 100vw;
  left: 0vw; 
}

.horizontal-inner--1 .slide.two .line,
.horizontal-inner--2 .slide.two .line,
.horizontal-inner--3 .slide.two .line {
  position: absolute;
  left: 100vw; 
  top: calc(50vh - 5px);
  width: 50vw;
  height: 10px;
  background: green;
}



.horizontal-inner--1 .slide.three,
.horizontal-inner--2 .slide.three,
.horizontal-inner--3 .slide.three {
  flex: 0 0 75vw;
  width: 75vw;
}

.horizontal-inner--1 .slide.three .bar,
.horizontal-inner--2 .slide.three .bar,
.horizontal-inner--3 .slide.three .bar {
  width: 37.5vw;
  left: 18.75vw; 
}
              
            
!

JS

              
                // Horizontal Scroll
gsap.registerPlugin(ScrollTrigger);

function createHorizontalScrollAnimation(hrzTrigger, container, sections, containerAnimations) {
  let scrollTL = gsap.timeline({
    scrollTrigger: {
      trigger: hrzTrigger,
      pin: true,
      start: "center center",
      scrub: true,
      invalidateOnRefresh: true,
      end: `+=${container.offsetWidth * 3}`
    }
  });

  let containerTL = gsap.timeline();

  containerAnimations.forEach(animation => {
    containerTL.fromTo(animation.selector, animation.fromTo, animation.to);
  });

  sections.forEach((section, index) => {
    containerAnimations.forEach(animation => {
      animation.targets = section.querySelector(animation.selector);
      scrollTL.add(animation);
    });

    if (index === sections.length - 1) {
      scrollTL.fromTo(section.querySelector('.bar'), {
        scaleX: 0
      }, {
        scaleX: 1,
        ease: "none",
        duration: 30
      });
    } else {
      scrollTL.fromTo(section.querySelector('.bar'), {
        scaleX: 0
      }, {
        scaleX: 1,
        ease: "none",
        duration: 30
      });

      scrollTL.to(sections, {
        x: () => `-=${section.offsetWidth}px`,
        ease: "none",
        duration: 30
      }, "+=0.0");
    }
  });

  scrollTL.add(() => {
    containerTL.play();
  }, "+=0.0");

  sections.forEach(section => {
    ScrollTrigger.create({
      trigger: section, // Use the section as the trigger for each container animation
      animation: containerTL,
      start: "left center",
      end: "right left",
      scrub: true
    });
  });
}

// Usage example for multiple horizontal-scroll divs
for (let i = 1; i <= 5; i++) {
  let hrzTrigger = document.querySelector(`.horizontal-scroll--${i}`);
  let container = document.querySelector(`.horizontal-inner--${i}`);
  let sections = Array.from(container.querySelectorAll(".slide"));

  let containerAnimations = [
    { selector: ".custom-animation", fromTo: { opacity: 0, x: 0 }, 
    to: { opacity: 1, x: 80, ease: "power1.out", delay: 1, duration: 1 }},
  // Add more animations as needed
  ];

  createHorizontalScrollAnimation(hrzTrigger, container, sections, containerAnimations);
}
              
            
!
999px

Console