Created
July 7, 2015 07:50
-
-
Save restlessmedia/d071113de839b13eb68f to your computer and use it in GitHub Desktop.
Defer image load
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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