forked from toddmotto/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Todd Motto
committed
Jan 27, 2014
1 parent
b926657
commit 2527343
Showing
5 changed files
with
142 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 self = store[i]; | ||
if (_inView(self)) { | ||
self.src = self.getAttribute('data-echo'); | ||
store.splice(i, 1); | ||
var length = store.length; | ||
if (length > 0) { | ||
for (var i = 0; i < length; i++) { | ||
var self = store[i]; | ||
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); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
{ | ||
"name": "Echo", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"homepage": "https://github.com/toddmotto/echo", | ||
"author": "Todd Motto", | ||
"year" : "2013", | ||
"description": "Lazy-loading with data-* attributes, offsets and throttle options", | ||
"description": "Lazy-loading with data-* attributes, offset and throttle options", | ||
"author": { | ||
"name": "Todd Motto", | ||
"email": "[email protected]", | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 self = store[i]; | ||
if (_inView(self)) { | ||
self.src = self.getAttribute('data-echo'); | ||
store.splice(i, 1); | ||
var length = store.length; | ||
if (length > 0) { | ||
for (var i = 0; i < length; i++) { | ||
var self = store[i]; | ||
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); |