mirror of https://github.com/toddmotto/echo.git
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.
41 lines
984 B
41 lines
984 B
/*! |
|
* Echo v1.2.0 |
|
* Lazy-loading images with data-* attributes |
|
* Project: https://github.com/toddmotto/echo |
|
* by Todd Motto: http://toddmotto.com |
|
* Copyright. MIT licensed. |
|
*/ |
|
window.Echo = (function (window, document, undefined) { |
|
|
|
'use strict'; |
|
|
|
var store; |
|
|
|
var _inView = function (img) { |
|
var coords = img.getBoundingClientRect(); |
|
return (coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight); |
|
}; |
|
|
|
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) { |
|
store.splice(i, 1); |
|
} |
|
} |
|
} |
|
}; |
|
|
|
var init = function () { |
|
store = document.querySelectorAll('[data-echo]'); |
|
_pollImages(); |
|
window.onscroll = _pollImages; |
|
}; |
|
|
|
return { |
|
init: init |
|
}; |
|
|
|
})(window, document);
|
|
|