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 class="smooth-scroll">
  
  <section class="red min-h-100" data-scroll-section>
    <div class="section-inner">Text</div>
  </section>
  
  <section class="blue sectionLeftAndRight min-h-100" data-scroll-section>
    <div class="section-inner">
      
      <div class="leftText">
            <p>START</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>LEFT TEXT</p>
            <p>END</p>
    </div>
      
      <div class="rightPinImage">
        <img src="https://picsum.photos/id/237/200/300" alt="">
      </div>
      
    </div>
  </section>
  
  <section class="green min-h-50" data-scroll-section>
    <div class="section-inner">Text</div>
  </section>
  
  <section class="purple min-h-100" data-scroll-section>
    <div class="section-inner">Text</div>
  </section>
  
  <section class="horizontalScrolling pink" data-scroll-section>
    <div class="section-inner">
      <div data-scroll-in-section>
        <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
                <div class="item"></div>
      </div>
    </div>
  </section>
  
  <section class="blue min-h-100 afterHorizontal" data-scroll-section>
    <div class="section-inner">Text</div>
  </section>

  <section class="green min-h-100" data-scroll-section>
    <div class="section-inner">Text</div>
  </section>
  
</div>
              
            
!

CSS

              
                body {
  font-family: sans-serif;
  overflow-x: hidden;
}

.smooth-scroll {
  overflow: hidden !important;
}

.min-h-50 {
  min-height: 50vh;
}

.min-h-100 {
  min-height: 100vh;
}

section {
  width: 100vw;
  color: white;
  font-size: 40px;
  font-weight: 800;
}


.red {
  background: red;
}

.blue {
  background: blue;
}

.green {
  background: green;
}

.purple {
  background: purple;
}

.pink {
  background: pink;
}

.horizontalScrolling {
  padding-top: 125px;
  padding-bottom: 125px;
}

.horizontalScrolling .section-inner {
  min-height: max-content;
}

[data-scroll-in-section] {
  display: inline-flex;
  padding-right: 50px;
}

.item {
  width: 200px;
  height: 200px;
  background-color: red;
}

.item + .item {
  margin-left: 25px;
}

.sectionLeftAndRight .section-inner {
  display: grid;
  grid-template-columns: 1fr 1fr;
}


section.afterHorizontal {
  margin-top: -100vh;
}
              
            
!

JS

              
                console.clear();

gsap.registerPlugin(ScrollTrigger);



// Using Locomotive Scroll from Locomotive https://github.com/locomotivemtl/locomotive-scroll

const locoScroll = new LocomotiveScroll({
  el: document.querySelector(".smooth-scroll"),
  smooth: true
});
// each time Locomotive Scroll updates, tell ScrollTrigger to update too (sync positioning)
locoScroll.on("scroll", ScrollTrigger.update);

// tell ScrollTrigger to use these proxy methods for the ".smooth-scroll" element since Locomotive Scroll is hijacking things
ScrollTrigger.scrollerProxy(".smooth-scroll", {
  scrollTop(value) {
    return arguments.length ? locoScroll.scrollTo(value, 0, 0) : locoScroll.scroll.instance.scroll.y;
  }, // we don't have to define a scrollLeft because we're only scrolling vertically.
  getBoundingClientRect() {
    return {top: 0, left: 0, width: window.innerWidth, height: window.innerHeight};
  },
  // LocomotiveScroll handles things completely differently on mobile devices - it doesn't even transform the container at all! So to get the correct behavior and avoid jitters, we should pin things with position: fixed on mobile. We sense it by checking to see if there's a transform applied to the container (the LocomotiveScroll-controlled element).
  pinType: document.querySelector(".smooth-scroll").style.transform ? "transform" : "fixed"
});



const sections = gsap.utils.toArray('section')

sections.forEach( function(section) {
  
  const inner = section.classList.contains('sectionLeftAndRight') ? section.querySelector('.leftText') : section.querySelector('.section-inner')
    
  if (!section.classList.contains('horizontalScrolling')) {

    ScrollTrigger.create({

      scroller: '.smooth-scroll',
      trigger: section,
      start: section.scrollHeight <= window.innerHeight ? 'top top' : 'bottom bottom',
      end: '+=100%',
      pin: inner,
      pinSpacing: false,
      pinType: 'transform'

    })
    
  } else {
   
    const scroll = section.querySelector('[data-scroll-in-section]');

    // the tween and the pinning have two different ScrollTriggers, because the will need different durations for that overlaying-effect to show
    
    ScrollTrigger.create({

      scroller: '.smooth-scroll',
      trigger: section,
      start: 'center center',
      end: () => `+=${section.scrollWidth + window.innerHeight}`, // added an extra window.innerHeight to the end here in comparison to the tween
      pin: inner,
      pinSpacing: true,
      pinType: 'transform',
      anticipatePin: 1,
      
    })
    
    gsap.to(scroll, {
      x: () => `${-(section.scrollWidth - document.documentElement.clientWidth)}px`,
      ease: 'none',
      scrollTrigger: {
        trigger: scroll,
        scroller: '.smooth-scroll',
        start: 'center center',
        end: () => `+=${section.scrollWidth}`,
        scrub: true,
      }
    });
    
  }

  
})

const timeline = gsap.timeline({
  scrollTrigger: {
    trigger: '.rightPinImage img',
    scroller: '.smooth-scroll',
    scrub: true,
    start: '50% 50%',
    endTrigger: '.sectionLeftAndRight .section-inner',
    end: 'bottom bottom',
    anticipatePin: 1,
    pin: true,
    pinSpacing: false,
    pinType: 'transform'
  }
});




// each time the window updates, we should refresh ScrollTrigger and then update LocomotiveScroll. 
ScrollTrigger.addEventListener("refresh", () => locoScroll.update());

// after everything is set up, refresh() ScrollTrigger and update LocomotiveScroll because padding may have been added for pinning, etc.
ScrollTrigger.refresh();
              
            
!
999px

Console