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 id="container">
  <h3>Lazy loading CSS background images</h3>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam at ligula non lorem facilisis finibus. Suspendisse turpis lacus, tempus sit amet dignissim sit amet, pretium non sem. Ut malesuada purus ac magna placerat, tempus commodo turpis consectetur. Nunc vitae metus vehicula nunc congue varius. Suspendisse egestas urna non finibus feugiat. Vestibulum maximus, nunc et tempus convallis, dui ante vestibulum est, in lobortis purus sapien eget lacus. Suspendisse cursus enim non quam mollis imperdiet. Pellentesque convallis eget nunc vitae fringilla. Fusce interdum, velit semper varius aliquet, eros sapien facilisis erat, iaculis vulputate erat arcu vel neque. Suspendisse dignissim hendrerit lorem. Etiam metus ex, finibus vitae iaculis vitae, iaculis non ex. Phasellus commodo id ante id consequat. Nunc consectetur viverra lacus non fringilla. Pellentesque nec nisl vulputate, sagittis nisl id, venenatis nisi. Nunc malesuada tellus a elit suscipit tempor.
  </p>
  <p>
Nam lacinia tortor quis volutpat lacinia. Aliquam in orci in nunc vehicula maximus. Phasellus elementum nulla augue, at aliquam sem pulvinar dapibus. Vivamus molestie venenatis risus pulvinar interdum. Phasellus blandit tortor eget nulla sagittis auctor. Cras sed leo in velit lobortis euismod. Suspendisse non ante tellus.
  </p>
  <div id="bg-image" class="lazy"></div>
  <p>
Nam lacinia tortor quis volutpat lacinia. Aliquam in orci in nunc vehicula maximus. Phasellus elementum nulla augue, at aliquam sem pulvinar dapibus. Vivamus molestie venenatis risus pulvinar interdum. Phasellus blandit tortor eget nulla sagittis auctor. Cras sed leo in velit lobortis euismod. Suspendisse non ante tellus.
  </p>
</div>
              
            
!

CSS

              
                * {
  font-family: sans-serif;
}

#container {
  font-size: 20px;
  line-height: 30px;
  max-width: 600px;
}

#bg-image.lazy {
   background-image: none;
   background-color: #F1F1FA;
}
#bg-image {
  background-image: url("https://ik.imagekit.io/demo/img/image10.jpeg?tr=w-600,h-400");
  max-width: 600px;
  height: 400px;
}
              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function() {
  var lazyloadImages;    

  if ("IntersectionObserver" in window) {
    lazyloadImages = document.querySelectorAll(".lazy");
    var imageObserver = new IntersectionObserver(function(entries, observer) {
      entries.forEach(function(entry) {
        if (entry.isIntersecting) {
          var image = entry.target;
          image.classList.remove("lazy");
          imageObserver.unobserve(image);
        }
      });
    });

    lazyloadImages.forEach(function(image) {
      imageObserver.observe(image);
    });
  } else {  
    var lazyloadThrottleTimeout;
    lazyloadImages = document.querySelectorAll(".lazy");
    
    function lazyload () {
      if(lazyloadThrottleTimeout) {
        clearTimeout(lazyloadThrottleTimeout);
      }    

      lazyloadThrottleTimeout = setTimeout(function() {
        var scrollTop = window.pageYOffset;
        lazyloadImages.forEach(function(img) {
            if(img.offsetTop < (window.innerHeight + scrollTop)) {
              img.src = img.dataset.src;
              img.classList.remove('lazy');
            }
        });
        if(lazyloadImages.length == 0) { 
          document.removeEventListener("scroll", lazyload);
          window.removeEventListener("resize", lazyload);
          window.removeEventListener("orientationChange", lazyload);
        }
      }, 20);
    }

    document.addEventListener("scroll", lazyload);
    window.addEventListener("resize", lazyload);
    window.addEventListener("orientationChange", lazyload);
  }
})


              
            
!
999px

Console