Image lazy loader
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
984 B

11 years ago
/*!
11 years ago
* Echo v1.2.0
* Lazy-loading images with data-* attributes
11 years ago
* Project: https://github.com/toddmotto/echo
11 years ago
* by Todd Motto: http://toddmotto.com
* Copyright. MIT licensed.
11 years ago
*/
11 years ago
window.Echo = (function (window, document, undefined) {
11 years ago
'use strict';
11 years ago
var store;
11 years ago
var _inView = function (img) {
var coords = img.getBoundingClientRect();
11 years ago
return (coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight);
11 years ago
};
11 years ago
var _pollImages = function () {
for (var i = 0; i < store.length; i++) {
var self = store[i];
if (_inView(self)) {
self.src = self.getAttribute('data-echo');
if ([].indexOf && store.indexOf(self) !== -1) {
11 years ago
store.splice(i, 1);
}
11 years ago
}
}
};
11 years ago
var init = function () {
store = document.querySelectorAll('[data-echo]');
11 years ago
_pollImages();
window.onscroll = _pollImages;
11 years ago
};
11 years ago
return {
init: init
};
})(window, document);