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

              
                 <div class="viewport">
    <div>
      <!-- Specifying a parallax rate of 0.3 will slide the image at that proportion of the scroll speed. -->
      <div class="parallax header" style="background-image: url('https://source.unsplash.com/WLUHO9A_xik/1600x900');" parallax="0.3"></div>
      <div class="scene header">
        <h1>If your going to parallax</h1>
        <h2>make it performant</code></h2>
      </div>
      <div class="scene solid">
        <p>
          This is not your average parallax scroller. It doesn't update background-position and doesn’t rely on JavaScript.  Works on both desktop and mobile and runs at 60fps. Find out what makes this tick <a href="https://developers.google.com/web/updates/2016/12/performant-parallaxing">
          here</a>
        </p>
      </div>
      <!-- When a rate isn't specified, it will be calculated such that the image always fills the space between the
           element before it and the next element with the "parallax-cover" attribute. -->
      <div class="parallax" style="background-image: url('https://source.unsplash.com/WJkc3xZjSXw/1328x996');" parallax></div>
      <div class="scene woods">
        <h1>Parallax may be a waste of space and bandwidth. But it looks really cool.</h1>
        <h2>Its here to stay, so at least do it right</h2>
      </div>
      <div class="scene solid woods" parallax-cover>
        <h2><b>Maths</b></h2>
        <p>
          Calculate amount scaled <code>(perspective - distance) / perspective.</code>
        </p>
        <p>
          For any content that doesn’t have a translateZ value applied, you can substitute a value of zero <code>(perspective - 0) / perspective</code>
        </p>
      </div>
      <!-- When a rate isn't specified and there is no subsequent element with the "parallax-cover" attribute the rate
           will be calculated such that the image slides until the end of the scrollable content. -->
      <div class="parallax" style="background-image: url('https://source.unsplash.com/4wzRuAb-KWs/1062x708');" parallax></div>
      <div class="scene sunset">
        <h1>Remember kids, Parallaxing can be fun, but can cause lots of performance issues</h1>
        <h2>Please use responsibly</h2>
      </div>
    </div>
  </div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Roboto:500,700');



$white: #fff;

* {
  box-sizing: border-box;
}

@mixin bp($point) {
  @if $point==full {
    //1080px 67.5em
    @media (min-width: 1080px) {
      @content;
    }
  } @else if $point==medium {
    //900px 56.25em
    @media (min-width: 768px) {
      @content;
    }
  } @else if $point==small {
    //550px 4.375em
    @media (min-width: 550px) {
      @content;
    }
  }
}

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  font-family: 'Roboto', sans-serif;
  color: $white;
  font-size: 1.25rem;
  letter-spacing: 1.5px;
  line-height: 1.5rem;
}

.viewport {
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  position: relative;
  -webkit-overflow-scrolling: touch;
}

h1 {
  font-size: 3rem;
  line-height: 3.5rem;
  
  @include bp (medium) {
     font-size: 4.5rem;
  line-height: 5rem; 
  }
}

h2 {
  font-size: 1.5rem;
  line-height: 1.75rem;
   @include bp (small) {
    font-size: 2.5rem;
      line-height: 2.75rem;
  }
}

strong,b {
  color:gold;
} 

code {
  background: #111;
  padding: 0 0.3rem;
}


.scene {
  height: 150vh;
  padding: 5vh 10vw;
  text-align:left;
}

.parallax {
  background-size: cover;
  box-sizing: border-box;
  width: 100%;
  height: 130vh;
  /* Since the parallax elements still consume space, we set the margin to undo
     the height of this parallax element making it essentially consume 0 space.
  */
  margin-bottom: -130vh;
}

.header {
  height: 70vh;
}

.parallax.header {
  margin-bottom: -70vh;
}

p {
  color: white;
  margin: 1em 0;
  z-index: 1;
  position: relative;
}

.scene:not(.header) {
  display: flex;
  flex-direction: column;
  justify-content: center;
  //align-items: center;
}


.solid {
  position: relative;
  display: flex;
  //flex-direction: column;
 // justify-content: center;
  align-items: flex-start;
  background: #222;
  height: 70vh;
  font-size: 1.5rem;
  line-height: 2rem;
    z-index: 1;
  &.star {
    background: #222;
  }
  &.woods {
      height: 80vh;
  }
}

  a {
    color: $white;
    text-decoration-skip: ink;
    text-decoration-color: #e6780e;
    transition: text-decoration-color 0.4s ease-in-out;
    // text-decoration-style: dashed;
    &:visited {
      color: $white;
      text-decoration-color: darken(#e6780e, 10%);
    }
    &:hover {
      text-decoration-color: #ccc;
      // background-color: $white;
      // color: $black;
    }
  }

              
            
!

JS

              
                function initializeParallax(clip) {
  var parallax = clip.querySelectorAll('*[parallax]');
  var parallaxDetails = [];
  var sticky = false;

  // Edge requires a transform on the document body and a fixed position element
  // in order for it to properly render the parallax effect as you scroll.
  // See https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/5084491/
  if (getComputedStyle(document.body).transform == 'none')
    document.body.style.transform = 'translateZ(0)';
  var fixedPos = document.createElement('div');
  fixedPos.style.position = 'fixed';
  fixedPos.style.top = '0';
  fixedPos.style.width = '1px';
  fixedPos.style.height = '1px';
  fixedPos.style.zIndex = 1;
  document.body.insertBefore(fixedPos, document.body.firstChild);

  for (var i = 0; i < parallax.length; i++) {
    var elem = parallax[i];
    var container = elem.parentNode;
    if (getComputedStyle(container).overflow != 'visible') {
      console.error('Need non-scrollable container to apply perspective for', elem);
      continue;
    }
    if (clip && container.parentNode != clip) {
      console.warn('Currently we only track a single overflow clip, but elements from multiple clips found.', elem);
    }
    var clip = container.parentNode;
    if (getComputedStyle(clip).overflow == 'visible') {
      console.error('Parent of sticky container should be scrollable element', elem);
    }
    // TODO(flackr): optimize to not redo this for the same clip/container.
    var perspectiveElement;
    if (sticky || getComputedStyle(clip).webkitOverflowScrolling) {
      sticky = true;
      perspectiveElement = container;
    } else {
      perspectiveElement = clip;
      container.style.transformStyle = 'preserve-3d';
    }
    perspectiveElement.style.perspectiveOrigin = 'bottom right';
    perspectiveElement.style.perspective = '1px';
    if (sticky)
      elem.style.position = '-webkit-sticky';
    if (sticky)
      elem.style.top = '0';
    elem.style.transformOrigin = 'bottom right';

    // Find the previous and next elements to parallax between.
    var previousCover = parallax[i].previousElementSibling;
    while (previousCover && previousCover.hasAttribute('parallax'))
      previousCover = previousCover.previousElementSibling;
    var nextCover = parallax[i].nextElementSibling;
    while (nextCover && !nextCover.hasAttribute('parallax-cover'))
      nextCover = nextCover.nextElementSibling;

    parallaxDetails.push({'node': parallax[i],
                          'top': parallax[i].offsetTop,
                          'sticky': !!sticky,
                          'nextCover': nextCover,
                          'previousCover': previousCover});
  }

  // Add a scroll listener to hide perspective elements when they should no
  // longer be visible.
  clip.addEventListener('scroll', function() {
    for (var i = 0; i < parallaxDetails.length; i++) {
      var container = parallaxDetails[i].node.parentNode;
      var previousCover = parallaxDetails[i].previousCover;
      var nextCover = parallaxDetails[i].nextCover;
      var parallaxStart = previousCover ? (previousCover.offsetTop + previousCover.offsetHeight) : 0;
      var parallaxEnd = nextCover ? nextCover.offsetTop : container.offsetHeight;
      var threshold = 200;
      var visible = parallaxStart - threshold - clip.clientHeight < clip.scrollTop &&
                    parallaxEnd + threshold > clip.scrollTop;
      // FIXME: Repainting the images while scrolling can cause jank.
      // For now, keep them all.
      // var display = visible ? 'block' : 'none'
      var display = 'block';
      if (parallaxDetails[i].node.style.display != display)
        parallaxDetails[i].node.style.display = display;
    }
  });
  window.addEventListener('resize', onResize.bind(null, parallaxDetails));
  onResize(parallaxDetails);
  for (var i = 0; i < parallax.length; i++) {
    parallax[i].parentNode.insertBefore(parallax[i], parallax[i].parentNode.firstChild);
  }
}

function onResize(details) {
  for (var i = 0; i < details.length; i++) {
    var container = details[i].node.parentNode;

    var clip = container.parentNode;
    var previousCover = details[i].previousCover;
    var nextCover = details[i].nextCover;
    var rate = details[i].node.getAttribute('parallax');

    var parallaxStart = previousCover ? (previousCover.offsetTop + previousCover.offsetHeight) : 0;
    var scrollbarWidth = details[i].sticky ? 0 : clip.offsetWidth - clip.clientWidth;
    var parallaxElem = details[i].sticky ? container : clip;
    var height = details[i].node.offsetHeight;
    var depth = 0;
    if (rate) {
      depth = 1 - (1 / rate);
    } else {
      var parallaxEnd = nextCover ? nextCover.offsetTop : container.offsetHeight;
      depth = (height - parallaxEnd + parallaxStart) / (height - clip.clientHeight);
    }
    if (details[i].sticky)
      depth = 1.0 / depth;

    var scale = 1.0 / (1.0 - depth);

    // The scrollbar is included in the 'bottom right' perspective origin.
    var dx = scrollbarWidth * (scale - 1);
    // Offset for the position within the container.
    var dy = details[i].sticky ?
        -(clip.scrollHeight - parallaxStart - height) * (1 - scale) :
        (parallaxStart - depth * (height - clip.clientHeight)) * scale;

    details[i].node.style.transform = 'scale(' + (1 - depth) + ') translate3d(' + dx + 'px, ' + dy + 'px, ' + depth + 'px)';
  }
}


  document.addEventListener('DOMContentLoaded', function() {
    initializeParallax(document.querySelector('.viewport'));
  });
              
            
!
999px

Console