Last active
August 29, 2015 14:24
-
-
Save ruffle1986/859df28e200098df574f to your computer and use it in GitHub Desktop.
Lazy Image Loader
This file contains hidden or 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
(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