cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <p><strong><a href="https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API" target="_blank">Intersection Observer</a> for Lazy Load Images</strong></p>

<p>Listening to scroll events is evil. <a href="https://caniuse.com/#feat=intersectionobserver" target="_blank">Unsupported browsers</a> fall back to loading all images immediately (Safari 😞 – Edge works fine 👌).</p>

<p>This demo includes optional <code>noscript</code> fallback and animated loading image.</p>

<div class="b-image">
  <div class="b-image__inner">
    <img
      src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/85421/intersection-observer-loading.svg"
      data-src="https://picsum.photos/640/360/?image=1"
      data-loaded="false" />
    <noscript>
      <img src="https://picsum.photos/640/360/?image=1" />
    </noscript>
  </div>
</div>
<!--/b-image-->


<script>
// Duplicating example image for demo purposes only!
// I'm lazy and I don't like HTML pre-processors – don't @ me 😡💢
var $image = document.querySelector('.b-image');
for (var i = 2; i < 21; i++) {
  var $newImage = $image.cloneNode(true);
  var $img = $newImage.querySelector('img[data-src]');
  $img.dataset.src = $img.dataset.src.replace('?image=1', '?image=' + i);
  $image.parentNode.appendChild($newImage);
}
</script>
            
          
!
            
              // Non of this CSS is strictly necessary
.b-image {
  background: #fff;
  margin-bottom: 1.5rem;
  position: relative;
  max-width: 40rem;
}

// Using an intrinsic ratio to maintain dimensions before the image has loaded
// this is not required for lazying loading but does avoid layout jumps
.b-image__inner {
  height: 0;
  padding-bottom: percentage(9/16);
  position: relative;

  img {
    height: 100%;
    left: 50%;
    position: absolute;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
  }
}

// House-keeping

body {
  background: #f2f0e6;
  padding: 1.5rem;
}

p {
  font-size: 18px;
  line-height: 1.35;
  margin: 0 0 1.5rem 0;
}

a {
  border-bottom: 2px solid rgba(#1d97bf, 0.1);
  color: #1d97bf;
  text-decoration: none;
  transition: border 150ms, color 150ms;

  &:focus,
  &:hover {
    border-color: #fffbcc;
    color: #39809e;
  }
}
            
          
!
            
              window.addEventListener('DOMContentLoaded', function() {
  // Find all images to lazy load
  var $images = [].slice.call(
    document.querySelectorAll('img[data-loaded="false"]')
  );
  // Keep a count of loaded images
  var loaded = 0;
  var observer = null;
  // Trigger an image to load
  var lazyLoad = function(image) {
    // Ignore if already loaded (or non-lazy)
    if (image.getAttribute('data-loaded') !== 'false') {
      return;
    }
    // Replace the loading src with the real thing
    image.src = image.getAttribute('data-src');
    image.setAttribute('data-loaded', 'true');
    if (!observer) {
      return;
    }
    // Unobserve th image and disconnect the Observer
    // if all images have now loaded
    observer.unobserve(image);
    if (++loaded >= $images.length) {
      observer.disconnect();
    }
  };
  if ('IntersectionObserver' in window) {
    // Setup the Observer
    observer = new IntersectionObserver(
      function(entries) {
        entries.forEach(function(entry) {
          // Trigger the image to load if within view
          if (entry.intersectionRatio > 0) {
            // `requestAnimationFrame` is not necessary
            // but it (probably) doesn't hurt?
            requestAnimationFrame(function() {
              lazyLoad(entry.target);
            });
          }
        });
      },
      {
        rootMargin: '50px 0px',
        threshold: 0.01
      }
    );
    // Start observing the lazy load images
    $images.forEach(function(img) {
      observer.observe(img);
    });
    // If Intersection Observer is unsupported
    // load all images immediately
  } else {
    $images.forEach(lazyLoad);
  }
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console