Skip to content

Instantly share code, notes, and snippets.

@ruffle1986
Last active August 29, 2015 14:24
Show Gist options
  • Save ruffle1986/859df28e200098df574f to your computer and use it in GitHub Desktop.
Save ruffle1986/859df28e200098df574f to your computer and use it in GitHub Desktop.
Lazy Image Loader
(function (factory) {
'use strict';
if (typeof exports !== 'undefined') {
module.exports = factory();
} else {
window.LazyImageLoader = factory();
}
}(function () {
return function init(host) {
var items = [].slice.call(document.querySelectorAll('.lazy-image'));
var len = items.length;
var sample;
items.forEach(function (item) {
if (!sample) {
sample = item.getBoundingClientRect();
}
var path = item.getAttribute('data-path');
var img = document.createElement('img');
img.src = host + '/' + Math.round(sample.width) + '/' + path;
item.appendChild(img);
});
};
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment