mirror of https://github.com/toddmotto/echo.git
Todd Motto
11 years ago
5 changed files with 142 additions and 43 deletions
@ -1,52 +1,102 @@
|
||||
/*! Echo v1.4.0 | (c) 2013 @toddmotto | MIT license | github.com/toddmotto/echo */ |
||||
window.Echo = (function (window, document, undefined) { |
||||
/*! Echo v1.5.0 | (c) 2014 @toddmotto | MIT license | github.com/toddmotto/echo */ |
||||
window.Echo = (function (global, document, undefined) { |
||||
|
||||
'use strict'; |
||||
|
||||
var store = [], offset, throttle, poll; |
||||
/** |
||||
* store |
||||
* @type {Array} |
||||
*/ |
||||
var store = []; |
||||
|
||||
var _inView = function (el) { |
||||
var coords = el.getBoundingClientRect(); |
||||
return ((coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight) + parseInt(offset)); |
||||
/** |
||||
* offset, throttle, poll vars |
||||
*/ |
||||
var offset, throttle, poll; |
||||
|
||||
/** |
||||
* _inView |
||||
* @private |
||||
* @param {Element} element Image element |
||||
* @returns {Boolean} Is element in viewport |
||||
*/ |
||||
var _inView = function (element) { |
||||
var coords = element.getBoundingClientRect(); |
||||
return ((coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight) + offset); |
||||
}; |
||||
|
||||
/** |
||||
* _pollImages Loop through the images if present |
||||
* or remove all event listeners |
||||
* @private |
||||
*/ |
||||
var _pollImages = function () { |
||||
for (var i = store.length; i--;) { |
||||
var length = store.length; |
||||
if (length > 0) { |
||||
for (var i = 0; i < length; i++) { |
||||
var self = store[i]; |
||||
if (_inView(self)) { |
||||
if (self && _inView(self)) { |
||||
self.src = self.getAttribute('data-echo'); |
||||
store.splice(i, 1); |
||||
length = store.length; |
||||
i--; |
||||
} |
||||
} |
||||
} else { |
||||
if (document.removeEventListener) { |
||||
global.removeEventListener('scroll', _throttle); |
||||
} else { |
||||
global.detachEvent('onscroll', _throttle); |
||||
} |
||||
clearTimeout(poll); |
||||
} |
||||
}; |
||||
|
||||
/** |
||||
* _throttle Sensible event firing |
||||
* @private |
||||
*/ |
||||
var _throttle = function () { |
||||
clearTimeout(poll); |
||||
poll = setTimeout(_pollImages, throttle); |
||||
}; |
||||
|
||||
/** |
||||
* init Module init function |
||||
* @param {Object} [obj] Passed in Object with options |
||||
* @param {Number|String} [obj.throttle] |
||||
* @param {Number|String} [obj.offset] |
||||
*/ |
||||
var init = function (obj) { |
||||
|
||||
var nodes = document.querySelectorAll('[data-echo]'); |
||||
var opts = obj || {}; |
||||
offset = opts.offset || 0; |
||||
throttle = opts.throttle || 250; |
||||
offset = parseInt(opts.offset || 0); |
||||
throttle = parseInt(opts.throttle || 250); |
||||
|
||||
for (var i = 0; i < nodes.length; i++) { |
||||
store.push(nodes[i]); |
||||
} |
||||
|
||||
_throttle(); |
||||
_pollImages(); |
||||
|
||||
if (document.addEventListener) { |
||||
window.addEventListener('scroll', _throttle, false); |
||||
global.addEventListener('scroll', _throttle, false); |
||||
global.addEventListener('load', _throttle, false); |
||||
} else { |
||||
window.attachEvent('onscroll', _throttle); |
||||
global.attachEvent('onscroll', _throttle); |
||||
global.attachEvent('onload', _throttle); |
||||
} |
||||
|
||||
}; |
||||
|
||||
/** |
||||
* return Public methods |
||||
* @returns {Object} |
||||
*/ |
||||
return { |
||||
init: init, |
||||
render: _throttle |
||||
render: _pollImages |
||||
}; |
||||
|
||||
})(window, document); |
||||
})(this, document); |
||||
|
@ -1,2 +1,2 @@
|
||||
/*! Echo v1.4.0 | (c) 2013 @toddmotto | MIT license | github.com/toddmotto/echo */ |
||||
window.Echo=function(a,b){"use strict";var c,d,e,f=[],g=function(d){var e=d.getBoundingClientRect();return(e.top>=0&&e.left>=0&&e.top)<=(a.innerHeight||b.documentElement.clientHeight)+parseInt(c)},h=function(){for(var a=f.length;a--;){var b=f[a];g(b)&&(b.src=b.getAttribute("data-echo"),f.splice(a,1))}},i=function(){clearTimeout(e),e=setTimeout(h,d)},j=function(e){var g=b.querySelectorAll("[data-echo]"),h=e||{};c=h.offset||0,d=h.throttle||250;for(var j=0;j<g.length;j++)f.push(g[j]);i(),b.addEventListener?a.addEventListener("scroll",i,!1):a.attachEvent("onscroll",i)};return{init:j,render:i}}(window,document); |
||||
/*! Echo v1.5.0 | (c) 2014 @toddmotto | MIT license | github.com/toddmotto/echo */ |
||||
window.Echo=function(a,b){"use strict";var c,d,e,f=[],g=function(a){var d=a.getBoundingClientRect();return(d.top>=0&&d.left>=0&&d.top)<=(window.innerHeight||b.documentElement.clientHeight)+c},h=function(){var c=f.length;if(c>0)for(var d=0;c>d;d++){var h=f[d];h&&g(h)&&(h.src=h.getAttribute("data-echo"),f.splice(d,1),c=f.length,d--)}else b.removeEventListener?a.removeEventListener("scroll",i):a.detachEvent("onscroll",i),clearTimeout(e)},i=function(){clearTimeout(e),e=setTimeout(h,d)},j=function(e){var g=b.querySelectorAll("[data-echo]"),j=e||{};c=parseInt(j.offset||0),d=parseInt(j.throttle||250);for(var k=0;k<g.length;k++)f.push(g[k]);h(),b.addEventListener?(a.addEventListener("scroll",i,!1),a.addEventListener("load",i,!1)):(a.attachEvent("onscroll",i),a.attachEvent("onload",i))};return{init:j,render:h}}(this,document); |
@ -1,51 +1,101 @@
|
||||
window.Echo = (function (window, document, undefined) { |
||||
window.Echo = (function (global, document, undefined) { |
||||
|
||||
'use strict'; |
||||
|
||||
var store = [], offset, throttle, poll; |
||||
/** |
||||
* store |
||||
* @type {Array} |
||||
*/ |
||||
var store = []; |
||||
|
||||
var _inView = function (el) { |
||||
var coords = el.getBoundingClientRect(); |
||||
return ((coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight) + parseInt(offset)); |
||||
/** |
||||
* offset, throttle, poll vars |
||||
*/ |
||||
var offset, throttle, poll; |
||||
|
||||
/** |
||||
* _inView |
||||
* @private |
||||
* @param {Element} element Image element |
||||
* @returns {Boolean} Is element in viewport |
||||
*/ |
||||
var _inView = function (element) { |
||||
var coords = element.getBoundingClientRect(); |
||||
return ((coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight) + offset); |
||||
}; |
||||
|
||||
/** |
||||
* _pollImages Loop through the images if present |
||||
* or remove all event listeners |
||||
* @private |
||||
*/ |
||||
var _pollImages = function () { |
||||
for (var i = store.length; i--;) { |
||||
var length = store.length; |
||||
if (length > 0) { |
||||
for (var i = 0; i < length; i++) { |
||||
var self = store[i]; |
||||
if (_inView(self)) { |
||||
if (self && _inView(self)) { |
||||
self.src = self.getAttribute('data-echo'); |
||||
store.splice(i, 1); |
||||
length = store.length; |
||||
i--; |
||||
} |
||||
} |
||||
} else { |
||||
if (document.removeEventListener) { |
||||
global.removeEventListener('scroll', _throttle); |
||||
} else { |
||||
global.detachEvent('onscroll', _throttle); |
||||
} |
||||
clearTimeout(poll); |
||||
} |
||||
}; |
||||
|
||||
/** |
||||
* _throttle Sensible event firing |
||||
* @private |
||||
*/ |
||||
var _throttle = function () { |
||||
clearTimeout(poll); |
||||
poll = setTimeout(_pollImages, throttle); |
||||
}; |
||||
|
||||
/** |
||||
* init Module init function |
||||
* @param {Object} [obj] Passed in Object with options |
||||
* @param {Number|String} [obj.throttle] |
||||
* @param {Number|String} [obj.offset] |
||||
*/ |
||||
var init = function (obj) { |
||||
|
||||
var nodes = document.querySelectorAll('[data-echo]'); |
||||
var opts = obj || {}; |
||||
offset = opts.offset || 0; |
||||
throttle = opts.throttle || 250; |
||||
offset = parseInt(opts.offset || 0); |
||||
throttle = parseInt(opts.throttle || 250); |
||||
|
||||
for (var i = 0; i < nodes.length; i++) { |
||||
store.push(nodes[i]); |
||||
} |
||||
|
||||
_throttle(); |
||||
_pollImages(); |
||||
|
||||
if (document.addEventListener) { |
||||
window.addEventListener('scroll', _throttle, false); |
||||
global.addEventListener('scroll', _throttle, false); |
||||
global.addEventListener('load', _throttle, false); |
||||
} else { |
||||
window.attachEvent('onscroll', _throttle); |
||||
global.attachEvent('onscroll', _throttle); |
||||
global.attachEvent('onload', _throttle); |
||||
} |
||||
|
||||
}; |
||||
|
||||
/** |
||||
* return Public methods |
||||
* @returns {Object} |
||||
*/ |
||||
return { |
||||
init: init, |
||||
render: _throttle |
||||
render: _pollImages |
||||
}; |
||||
|
||||
})(window, document); |
||||
})(this, document); |
||||
|
Loading…
Reference in new issue