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

              
                	<p>This example shows a pure JS (no jQuery or shit) function that loads the images only they are currently in the visible area of the window.</p>
	<p>You can open your dev-tools and monitor how the requests are made gradually when you scroll.</p>
	<p>Note 1: only vertical scrolling is monitored here.<br/>
	Note 2: for rendering optimization, images start to load 500px under the visibility line and 50px above. You can edit this if you want to.</p>

		<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&1" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&2" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&3" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&4" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&5" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&6" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&7" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&8" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&9" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&11" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&12" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&13" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&14" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&15" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>
	<img data-src="https://picsum.photos/1000/800/?random&16" class="lazyload" width="1000" height="800" alt="random dummy image" />
	<hr/>


              
            
!

CSS

              
                img {
	max-width: 100%;
	height: auto;
	box-shadow: 5px 5px 50px black;
}

hr {
	margin: 200px auto;
}

body {
	max-width: 1000px;
	margin: auto;
}


              
            
!

JS

              
                	window.addEventListener("scroll", function() { onScrollDiv() });
	window.addEventListener("DOMContentLoaded", function() { onScrollDiv() });

	function onScrollDiv() {
		var images = document.querySelectorAll('.lazyload');
		for (var i=0, nb=images.length ; i <nb ; i++) {
			var img = images[i]
			var rect = img.getBoundingClientRect();
			var isVisible = ((rect.top - window.innerHeight) < 500 && (rect.bottom) > -50 ) ? true : false ;

			if (isVisible) {
				if (!img.src) {
					img.src = img.dataset.src;
				}
			}
		}
	}

              
            
!
999px

Console