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="swipe-section">
  <section class="panel red vh-200">
            <button>click me to make this section scroll vertically</button>
    Button to unpin this section so it can scroll vertically, then at the end of the vertical scroll, the next section (panel 2) slides over. 
  </section>
  <section class="panel purple x-100">
    If you clicked the button and now want to scroll back to panel 1, you will slide back to the top of panel 1
 </section>
  <section class="panel blue x-100">
     Section 3
  </section>
  <section class="panel orange x-100 vh-200">
    Section 4
  </section>
</div>





              
            
!

CSS

              
                body {
  overscroll-behavior: none;
  min-height: 100vh;
}

.swipe-section {
  position: relative;
  height: 100%;
  width: 100%;
  overflow: visible;
}

.swipe-section .panel {
  position: absolute;
}

.vh-200 {
  height: 500vh !important;
  display:flex;
  justify-content:flex-start;
  align-items:flex-start;
}
              
            
!

JS

              
                gsap.registerPlugin(ScrollTrigger);

// Gsap Code
let currentIndex = -1;
let animating;
let swipePanels = gsap.utils.toArray(".swipe-section .panel");

// set second panel two initial 100%
gsap.set(".x-100", {xPercent: 100});

// set z-index levels for the swipe panels
gsap.set(swipePanels, {
  zIndex: i => i
});

// create an observer and disable it to start
let intentObserver = ScrollTrigger.observe({
  type: "wheel,touch",
  onUp: () => !animating && gotoPanel(currentIndex - 1, false),
  onDown: () => !animating && gotoPanel(currentIndex + 1, true),
  tolerance: 10,
  preventDefault: true
})
intentObserver.disable();

// handle the panel swipe animations
function gotoPanel(index, isScrollingDown) {
  animating = true;
  // return to normal scroll if we're at the end or back up to the start
  if ((index === swipePanels.length && isScrollingDown) || (index === -1 && !isScrollingDown)) {

        let target = index;
        gsap.to(target, {
        // xPercent: isScrollingDown ? -100 : 0,
        duration: 0.00,
        onComplete: () => {
          animating = false;
          isScrollingDown && intentObserver.disable();
        }
    });
    return
  }

//   target the second panel, last panel?
  let target = isScrollingDown ?  swipePanels[index]: swipePanels[currentIndex];

  gsap.to(target, {
    xPercent: isScrollingDown ? 0 : 100,
    duration: 0.75,
    onComplete: () => {
      animating = false;
    }
  });
  currentIndex = index;
  console.log(index);

}

// pin swipe section and initiate observer
ScrollTrigger.create({
  trigger: ".swipe-section",
  pin: true,
  start: "top top",
  end: "+=1",
  onEnter: () => {
    intentObserver.enable();
    gotoPanel(currentIndex + 1, true);    
  },
  onEnterBack: () => {
    intentObserver.enable();
    gotoPanel(currentIndex - 1, false);
  }
})

// make sure scrolltrigger doesnt fire onLoad
let hasScrolled = false;
function checkIfScrolled() {
  if(!hasScrolled) {
    queuedAnims.push(this.parent.pause(0));
  }
}

$("button").click(function(){
  intentObserver.disable();
});

              
            
!
999px

Console