Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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="viewport">
    
  <div id="scroll-container" class="scroll-container">
    <div class="content">
      
<!--       <div class="piece" data-depth="0"></div>
      <div class="piece half right empty" data-depth="10">
        <span class="big-char">5</span>
        <p class="text">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Nullam id dolor id nibh ultricies vehicula ut id elit. Nullam quis risus eget urna mollis ornare vel eu leo. Etiam porta sem malesuada magna mollis euismod. Maecenas sed diam eget risus varius blandit sit amet non magna.</p>
      </div>
      <div class="piece half" data-depth="0"></div>
      <div class="piece" data-depth="10"></div>
      <div class="piece" data-depth="0"></div>
      <div class="piece half right" data-depth="10"></div>
      <div class="piece half empty" data-depth="0">
        <span class="med-char">Space</span>    
        <p class="text">Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vestibulum id ligula porta felis euismod semper. Nulla vitae elit libero, a pharetra augue. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Aenean lacinia bibendum nulla sed consectetur.</p>
      </div>
      <div class="piece" data-depth="10"></div>
      <div class="piece" data-depth="0"></div> -->
      
<!--       <div class="item photo-wrapper" data-depth="0">
        <img class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-10.jpg">
      </div>
      
      <div class="item right photo-wrapper" data-depth="10">
        <img class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-14.jpg">
      </div>
      
      <div class="item photo-wrapper" data-depth="10">
        <img class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-15.jpg">
      </div>
      
      <div class="item half photo-wrapper" data-depth="10">
        <img class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-16.jpg">
      </div>
      
      <div class="item right photo-wrapper" data-depth="10">
        <img class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-10.jpg">
      </div>
      
      <div class="item half photo-wrapper" data-depth="10">
        <img class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-14.jpg">
      </div> -->
      
      <div class="item photo-wrapper">
        <img data-depth="0" class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-10.jpg">
      </div>
      
      <div class="item right photo-wrapper">
        <img data-depth="5" class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-14.jpg">
      </div>
      
      <div class="item photo-wrapper">
        <img data-depth="10" class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-15.jpg">
      </div>
      
      <div class="item half photo-wrapper">
        <img data-depth="0" class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-16.jpg">
      </div>
      
      <div class="item right photo-wrapper">
        <img data-depth="10" class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-10.jpg">
      </div>
      
      <div class="item half photo-wrapper">
        <img data-depth="5" class="photo" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/splash-14.jpg">
      </div>
      
  </div>   
</section>

              
            
!

CSS

              
                *, :after, :before {
    box-sizing: border-box;
}

body {
  overflow-x: hidden;
  overflow-y: scroll;
}

.viewport {
  overflow: hidden;
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.scroll-container {  
  position: absolute;  
  overflow: hidden;
  z-index: 10;
  backface-visibility: hidden;
  transform-style: preserve-3d;
  width: 100%;
}

.content {
  padding: 50px;
  overflow: hidden;
  position: relative;
  width: 100%;
}

.photo-wrapper {
  position: relative;
  overflow: hidden;  
  backface-visibility: hidden;
  transform-style: preserve-3d;  
  background: #ddd;
}

.photo {  
  position: absolute;
  z-index: 20;
  width: 150%;
  height: auto;
  top: 0;
  left: 0;  
}

.item {
  position: relative;
  height: 500px;
  float: left;
  width: 100%;
  clear: both;
  
  & + & {
    margin-top: -100px;
    margin-top: 100px;
  }
  
  &.half {
    width: 50%;
  }
  
  &.right {
    float: right;
    width: 75%;
  }
}
              
            
!

JS

              
                console.clear();

class SmoothScroll {
  
  constructor(options) {
    
    this.target = options.target;
    
    this.scrollEase = options.scrollEase != null ? options.scrollEase : 0.1;
    this.maxOffset = options.maxOffset != null ? options.maxOffset : 500;
    
    this.endThreshold = 0.05;    
    this.requestId = null;    
    this.maxDepth = 10;    
    this.viewHeight = 0;
    this.halfViewHeight = 0;
    this.maxDistance = 0;
    this.scrollHeight = 0;            
    this.endScroll = 0;
    this.currentScroll = 0;    
    this.resizeRequest = 1;
    this.scrollRequest = 0;    
    this.scrollItems = [];
    
    this.addItems();
    
    window.addEventListener("resize", this._onResize);
    window.addEventListener("scroll", this._onScroll);
        
    this._update();
  }
  
  _onResize = (event) => {
    this.resizeRequest++;
    if (!this.requestId) {
      this.requestId = requestAnimationFrame(this._update);
    }
  }
  
  _onScroll = (event) => {
    this.scrollRequest++;
    if (!this.requestId) {
      this.requestId = requestAnimationFrame(this._update);
    }
  }
  
  _update = () => {
    
    const resized = this.resizeRequest > 0;    
    const scrollY = window.pageYOffset;
        
    if (resized) {    
      const height = this.target.clientHeight;
      document.body.style.height = height + "px";      
      this.scrollHeight = height;
      this.viewHeight = window.innerHeight;
      this.halfViewHeight = this.viewHeight / 2;
      this.maxDistance = this.viewHeight * 2;            
      this.resizeRequest = 0;
    }    
    
    this.endScroll = scrollY;
    this.currentScroll += (scrollY - this.currentScroll) * this.scrollEase;

    if (Math.abs(scrollY - this.currentScroll) < this.endThreshold || resized) {
      this.currentScroll = scrollY;
      this.scrollRequest = 0;
    }
    
    // const scrollOrigin = scrollY + this.halfViewHeight;
    const scrollOrigin = this.currentScroll + this.halfViewHeight;
    
    this.target.style.transform = `translate3d(0px,-${this.currentScroll}px,0px)`;

    for (let i = 0; i < this.scrollItems.length; i++) {
      const item = this.scrollItems[i];
      
      const distance = scrollOrigin - item.top;
      const offsetRatio = distance / this.maxDistance;
      
      item.endOffset = Math.round(this.maxOffset * item.depthRatio * offsetRatio);
      
      if (Math.abs(item.endOffset - item.currentOffset < this.endThreshold)) {
        item.currentOffset = item.endOffset;
      } else {
        item.currentOffset += (item.endOffset - item.currentOffset) * this.scrollEase;
      }
      
      item.target.style.transform = `translate3d(0px,-${item.currentOffset}px,0px)`;
    }
        
    this.requestId = this.scrollRequest > 0 ? requestAnimationFrame(this._update) : null;
  }
  
  addItems() {
    
    this.scrollItems = [];
    const elements = document.querySelectorAll("*[data-depth]");
    
    for (let i = 0; i < elements.length; i++) {
      const element = elements[i];
      const depth = +element.getAttribute("data-depth");
      const rect = element.getBoundingClientRect();
      const item = {
        target: element,
        depth: depth,
        top: rect.top + window.pageYOffset,
        depthRatio: depth / this.maxDepth,
        currentOffset: 0,
        endOffset: 0
      };
      this.scrollItems.push(item);
    }
    
    return this;
  }
}

var scroller = new SmoothScroll({
  target: document.querySelector("#scroll-container"), // element container to scroll
  scrollEase: 0.1, // scroll speed
  maxOffset: 500 
});
              
            
!
999px

Console