Skip to content

Instantly share code, notes, and snippets.

@restlessmedia
Created July 7, 2015 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save restlessmedia/d071113de839b13eb68f to your computer and use it in GitHub Desktop.
Save restlessmedia/d071113de839b13eb68f to your computer and use it in GitHub Desktop.
Defer image load
var load = function (img) {
var src = img.getAttribute('data-src');
if (src && img.src !== src) {
img.src = src;
}
};
var find = function () {
return document.querySelectorAll('.js-defer');
};
var loadAll = function () {
var items = find();
var i = items.length;
while (i--) {
load(items[i]);
}
};
var bind = function (src, type, fn) {
src.addEventListener(type, fn, false);
};
var handleLoad = function () {
setTimeout(loadAll, 1000);
};
bind(window, 'load', handleLoad);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment