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

              
                <header>
  <h1>Fake Horizontal-Scroll with Fake-Pinning</h1>
</header>

<div class="container">
   <div class="wrapper">
     <div class="thumbnail">1</div>
     <div class="thumbnail fakePin">2</div>
     <div class="thumbnail full fakePin">3</div>
     <div class="thumbnail fakePin">4</div>
     <div class="thumbnail fakePin">5</div>
     <div class="thumbnail fakePin">6</div>
     <div class="thumbnail full fakePin">7</div>
     <div class="thumbnail fakePin">8</div> 
     <div class="thumbnail fakePin">9</div>
     <div class="thumbnail full fakePin">10</div>
     <div class="thumbnail fakePin">11</div>
   </div>
</div>

<footer>
  <div class="scrollbar"></div>
</footer>
              
            
!

CSS

              
                body {
  overflow-x: hidden;
  margin: 0;
  padding: 0;
}

header {
  position: fixed;
  z-index: 1000;
  margin-left: 10px;
}

h1 {
  font-size: 25px;
}

footer {
  height: 10vh;
  position: fixed;
  bottom: 0;
}

.scrollbar {
  position: relative;
  border-radius: 25px;
  //padding: 2.5px;
  color: white;
  background-color: #333;
  width: 150px;
  height: 10px;
  top: calc(5vh + 5px);
  left: 10px;
  text-align: center;
  cursor: pointer;
}

::-webkit-scrollbar {
  display: none;
}






.container {
  //background-color: #000;
}

.wrapper {  
  //display: flex;
  position: relative;
  
  height: 80vh;
  
  // all 800px elements + all 100vw elements + margins-right on all but the last element
  width: calc( (800px * 8) + ((100vw - 20px) * 3) + (10px * 10) );
  
  padding: 0px 10px;
  
  border-top: 10vh white solid;
  border-bottom: 10vh white solid; 
}

.thumbnail {
  display: inline-block;
  float: left;
  
  height: 80vh;
  
  // will become 800px in width because of the padding
  width: calc(800px - 20px);
  color: white;

  padding: 10px;
  font-weight: 800;
  
  border-radius: 10px;
  
  margin-right: 10px;
  
  // opacity: 0.8;
}

.thumbnail:nth-child(1) {
    background-color: red;
}

.thumbnail:nth-child(2) {
    background-color: navy;
}

.thumbnail:nth-child(3) {
    background-color: gray;
}

.thumbnail:nth-child(4) {
    background-color: orange;
}

.thumbnail:nth-child(5) {
    background-color: purple;
}

.thumbnail:nth-child(6) {
    background-color: black;
}

.thumbnail:nth-child(7) {
    background-color: green;
}

.thumbnail:nth-child(8) {
    background-color: darkred;
}

.thumbnail:nth-child(9) {
    background-color: teal;
}

.thumbnail:nth-child(10) {
    background-color: steelblue;
}

.thumbnail:nth-child(11) {
    background-color: salmon;
}

.thumbnail:last-child {
  margin-right: 0px;
}



.full {
  // the second extra '-20px' on here is just related to the fake-pinning and having space to the right side, when it happens
  width: calc(100vw - 20px - 20px);
}
              
            
!

JS

              
                gsap.registerPlugin(ScrollTrigger);

//const scrubValue = true;
const scrubValue = 0.5;

let container = document.querySelector('.container')

const scrollBar = gsap.to('.scrollbar', { x: () => { return window.innerWidth - (150 + 20) }, ease: "none" })

ScrollTrigger.create({
    trigger: ".container",
    start: "top top",
    end: () => (container.scrollWidth - window.innerWidth),
    pin: true,
    anticipatePin: 1,
    scrub: scrubValue,
    animation: scrollBar,
    invalidateOnRefresh: true,
})



let thumbNails = gsap.utils.toArray(".thumbnail");

thumbNails.forEach((thumb, i) => {
 
  if (thumb.classList.contains('fakePin')) {
                
    function prevAll(element) {
      var result = [];

      while (element = element.previousElementSibling)
          result.push(element);
      return result;
    }    
    
    // console.log(prevAll(thumb))
    
    var totalWidthToMove;
    
    function getTotalWidthToMove() {
        
      totalWidthToMove = 0;
      
      prevAll(thumb).forEach((thumbBefore, i) => {

        let style = thumbBefore.currentStyle || window.getComputedStyle(thumbBefore);      
        let marginRight = parseInt(style.marginRight);

        totalWidthToMove += thumbBefore.offsetWidth + marginRight;

      });    

      return totalWidthToMove;
      
    }
    //getTotalWidthToMove();
    //console.log(totalWidthToMove)
    
    gsap.to(thumb, {
      x: () => { return - getTotalWidthToMove() },
      ease: "none",
      scrollTrigger: {
        trigger: ".wrapper",
        start: 'top top',
        scrub: scrubValue,
        invalidateOnRefresh: true,
        end: () => "+=" + getTotalWidthToMove(),
      }
    });
    
  }
  else {
    
    gsap.to(thumb, {
      x: () => { return - (container.scrollWidth) },
      ease: "none",
      scrollTrigger: {
        trigger: ".wrapper",
        start: 'top top',
        scrub: scrubValue,
        invalidateOnRefresh: true,
        end: () => "+=" + (container.scrollWidth),
      }
    });
    
  }
    
  //console.log(thumb.offsetWidth)
  
});
              
            
!
999px

Console