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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                 <!DOCTYPE html>
<html>
  <head>
    <title>Lazy loading images</title>
  </head>
  <body>
    <h3>Hello Lazy Loading</h3>
    <h5>Have some text</h5>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <h3>Silhouette Of People</h3>
    <h5>Have some fun</h5>
    <div class="wrap-image">
      <img class="lazy-img" src="https://res.cloudinary.com/dup54scz6/image/upload/v1553402067/lazy-demo/silhouette-of-people-temp.jpg" data-src="https://res.cloudinary.com/dup54scz6/image/upload/v1553402067/lazy-demo/silhouette-of-people.jpg" data-srcset="https://res.cloudinary.com/dup54scz6/image/upload/v1553402067/lazy-demo/silhouette-of-people.jpg 1x" alt="Silhouette Of People" />
    </div>
    <h3>Wind Power</h3>
    <h5>Generate energy by harnessing wind power</h5>
    <div class="wrap-image">
      <img class="lazy-img" src="https://res.cloudinary.com/dup54scz6/image/upload/v1553402067/lazy-demo/wind-power-temp.jpg" data-src="https://res.cloudinary.com/dup54scz6/image/upload/v1553402067/lazy-demo/wind-power-1x.jpg" data-srcset="https://res.cloudinary.com/dup54scz6/image/upload/v1553402068/lazy-demo/wind-power-2x.jpg 2x, https://res.cloudinary.com/dup54scz6/image/upload/v1553402067/lazy-demo/wind-power-1x.jpg 1x" alt="Generate energy by harnessing wind power" />
    </div>
  </body>
</html>
              
            
!

CSS

              
                .wrap-image img {
  width: 450px;
}

              
            
!

JS

              
                // JavaScript implementation without jQuery
document.addEventListener( "DOMContentLoaded", initLazyObserver );

function initLazyObserver() {

	var images = [].slice.call( document.querySelectorAll( 'img.lazy-img' ) );

	// Insersection Observer support
	if( "IntersectionObserver" in window ) {

		intersectionObserver = new IntersectionObserver( function( entries, observer ) {

			entries.forEach( function( entry ) {

				if( entry.isIntersecting ) {

					image = entry.target;
					image.src = image.dataset.src;
					image.srcset = image.dataset.srcset;

					image.classList.remove( '.lazy-img' );

					observer.unobserve( image );
				}
			});
		});

		images.forEach( function( lazyImage ) {

			intersectionObserver.observe( lazyImage );
		});
	}
	else {

		// Use fallback using scroll listener
    initLazyListener( images );
	}
}

function initLazyListener( images ) {

	// Flag to relax the listener from continuous checking
	var active = false;

	// The actual listener to listen for scroll, resize and change in viewport orientation
	var lazyLoadListener = function() {

		if( active === false ) {

			active = true;

			setTimeout( function() {

				// Iterate over the images collection for lazy loading
				images.forEach( function( image ) {

					// Window intersection test
					if( ( image.getBoundingClientRect().top <= window.innerHeight && image.getBoundingClientRect().bottom >= 0 ) && getComputedStyle( image ).display !== "none" ) {

						image.src = image.dataset.src;
						image.srcset = image.dataset.srcset;

						// Done lazy loading
						image.classList.remove( '.lazy-img' );

						// Remove from collection
						images = images.filter( function( img ) {

							return image !== img;
						});

						// Stop lazy loading
						if( images.length === 0 ) {

							document.removeEventListener( 'scroll', lazyLoadListener );
							window.removeEventListener( 'resize', lazyLoadListener );
							window.removeEventListener( 'orientationchange', lazyLoadListener );
						}
					}
				});

				active = false;
			}, 200 );
		}
	};

	document.addEventListener( 'scroll', lazyLoadListener );
	window.addEventListener( 'resize', lazyLoadListener );
	window.addEventListener( 'orientationchange', lazyLoadListener );
}

              
            
!
999px

Console