Skip to content

Instantly share code, notes, and snippets.

@remainstheday
Created November 14, 2016 16:00
Show Gist options
  • Save remainstheday/43a97f8c5eb864a08a58b10009a6fd2a to your computer and use it in GitHub Desktop.
Save remainstheday/43a97f8c5eb864a08a58b10009a6fd2a to your computer and use it in GitHub Desktop.
Load images after the page has loaded to avoid lag
/**
* This script wrapped in a Immediately-Invoked Function Expression (IIFE) to
* prevent variables from leaking onto the global scope. For more information
* on IIFE visit the link below.
* @see http://en.wikipedia.org/wiki/Immediately-invoked_function_expression
*/
(function() {
'use strict';
// Load all images via Squarespace's Responsive ImageLoader
function loadAllImages() {
var images = document.querySelectorAll('img[data-src]' );
for (var i = 0; i < images.length; i++) {
ImageLoader.load(images[i], {load: true});
}
}
// The event subscription that loads images when the page is ready
document.addEventListener('DOMContentLoaded', loadAllImages);
// The event subscription that reloads images on resize
window.addEventListener('resize', loadAllImages);
}());
@remainstheday
Copy link
Author

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Omnis maxime, esse debitis corporis dolore id officiis laborum officia ducimus neque sit culpa praesentium, hic dolores voluptas nobis amet et? Numquam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment