Browse Source

Add option 'container' to support other containers than default window

pull/64/head
Arnaud JAEGERS 11 years ago
parent
commit
f992ff3589
  1. 10
      README.md
  2. 7
      dist/echo.js
  3. 2
      dist/echo.min.js
  4. 10
      package.json
  5. 7
      src/echo.js

10
README.md

@ -99,6 +99,16 @@ echo.init({
} }
}); });
``` ```
#### container
Type: `Object` Default: `null`
This allows you to specify a different container of the defaut 'document'.
```js
echo.init({
container: document.querySelector(".main")
});
```
## .render() ## .render()

7
dist/echo.js vendored

@ -17,7 +17,7 @@
var callback = function () {}; var callback = function () {};
var offset, poll, delay, useDebounce, unload; var offset, poll, delay, useDebounce, unload, container;
var inView = function (element, view) { var inView = function (element, view) {
var box = element.getBoundingClientRect(); var box = element.getBoundingClientRect();
@ -53,6 +53,7 @@
useDebounce = opts.debounce !== false; useDebounce = opts.debounce !== false;
unload = !!opts.unload; unload = !!opts.unload;
callback = opts.callback || callback; callback = opts.callback || callback;
container = opts.container || root;
echo.render(); echo.render();
if (document.addEventListener) { if (document.addEventListener) {
root.addEventListener('scroll', debounceOrThrottle, false); root.addEventListener('scroll', debounceOrThrottle, false);
@ -70,8 +71,8 @@
var view = { var view = {
l: 0 - offset.l, l: 0 - offset.l,
t: 0 - offset.t, t: 0 - offset.t,
b: (root.innerHeight || document.documentElement.clientHeight) + offset.b, b: (container.innerHeight || container.clientHeight) + offset.b,
r: (root.innerWidth || document.documentElement.clientWidth) + offset.r r: (container.innerWidth || container.clientWidth) + offset.r
}; };
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
elem = nodes[i]; elem = nodes[i];

2
dist/echo.min.js vendored

@ -1,2 +1,2 @@
/*! echo.js v1.6.0 | (c) 2014 @toddmotto | https://github.com/toddmotto/echo */ /*! echo.js v1.6.0 | (c) 2014 @toddmotto | https://github.com/toddmotto/echo */
!function(t,e){"function"==typeof define&&define.amd?define(function(){return e(t)}):"object"==typeof exports?module.exports=e:t.echo=e(t)}(this,function(t){"use strict";var e,n,o,r,c,i={},l=function(){},a=function(t,e){var n=t.getBoundingClientRect();return n.right>=e.l&&n.bottom>=e.t&&n.left<=e.r&&n.top<=e.b},d=function(){(r||!n)&&(clearTimeout(n),n=setTimeout(function(){i.render(),n=null},o))};return i.init=function(n){n=n||{};var a=n.offset||0,u=n.offsetVertical||a,f=n.offsetHorizontal||a,s=function(t,e){return parseInt(t||e,10)};e={t:s(n.offsetTop,u),b:s(n.offsetBottom,u),l:s(n.offsetLeft,f),r:s(n.offsetRight,f)},o=s(n.throttle,250),r=n.debounce!==!1,c=!!n.unload,l=n.callback||l,i.render(),document.addEventListener?(t.addEventListener("scroll",d,!1),t.addEventListener("load",d,!1)):(t.attachEvent("onscroll",d),t.attachEvent("onload",d))},i.render=function(){for(var n,o,r=document.querySelectorAll("img[data-echo]"),d=r.length,u={l:0-e.l,t:0-e.t,b:(t.innerHeight||document.documentElement.clientHeight)+e.b,r:(t.innerWidth||document.documentElement.clientWidth)+e.r},f=0;d>f;f++)o=r[f],a(o,u)?(c&&o.setAttribute("data-echo-placeholder",o.src),o.src=o.getAttribute("data-echo"),c||o.removeAttribute("data-echo"),l(o,"load")):c&&(n=o.getAttribute("data-echo-placeholder"))&&(o.src=n,o.removeAttribute("data-echo-placeholder"),l(o,"unload"));d||i.detach()},i.detach=function(){document.removeEventListener?t.removeEventListener("scroll",d):t.detachEvent("onscroll",d),clearTimeout(n)},i}); !function(t,e){"function"==typeof define&&define.amd?define(function(){return e(t)}):"object"==typeof exports?module.exports=e:t.echo=e(t)}(this,function(t){"use strict";var e,n,o,r,c,i,a={},l=function(){},d=function(t,e){var n=t.getBoundingClientRect();return n.right>=e.l&&n.bottom>=e.t&&n.left<=e.r&&n.top<=e.b},u=function(){(r||!n)&&(clearTimeout(n),n=setTimeout(function(){a.render(),n=null},o))};return a.init=function(n){n=n||{};var d=n.offset||0,f=n.offsetVertical||d,s=n.offsetHorizontal||d,h=function(t,e){return parseInt(t||e,10)};e={t:h(n.offsetTop,f),b:h(n.offsetBottom,f),l:h(n.offsetLeft,s),r:h(n.offsetRight,s)},o=h(n.throttle,250),r=n.debounce!==!1,c=!!n.unload,l=n.callback||l,i=n.container||t,a.render(),document.addEventListener?(t.addEventListener("scroll",u,!1),t.addEventListener("load",u,!1)):(t.attachEvent("onscroll",u),t.attachEvent("onload",u))},a.render=function(){for(var t,n,o=document.querySelectorAll("img[data-echo]"),r=o.length,u={l:0-e.l,t:0-e.t,b:(i.innerHeight||i.clientHeight)+e.b,r:(i.innerWidth||i.clientWidth)+e.r},f=0;r>f;f++)n=o[f],d(n,u)?(c&&n.setAttribute("data-echo-placeholder",n.src),n.src=n.getAttribute("data-echo"),c||n.removeAttribute("data-echo"),l(n,"load")):c&&(t=n.getAttribute("data-echo-placeholder"))&&(n.src=t,n.removeAttribute("data-echo-placeholder"),l(n,"unload"));r||a.detach()},a.detach=function(){document.removeEventListener?t.removeEventListener("scroll",u):t.detachEvent("onscroll",u),clearTimeout(n)},a});

10
package.json

@ -6,15 +6,15 @@
"license": "MIT", "license": "MIT",
"homepage": "https://github.com/toddmotto/echo", "homepage": "https://github.com/toddmotto/echo",
"devDependencies": { "devDependencies": {
"gulp": "~3.7.0", "gulp": "^3.8.7",
"gulp-uglify": "~0.3.0",
"gulp-rename": "~1.1.0",
"gulp-clean": "^0.2.4", "gulp-clean": "^0.2.4",
"gulp-plumber": "~0.6.2",
"gulp-header": "^1.0.2", "gulp-header": "^1.0.2",
"gulp-jshint": "^1.6.1", "gulp-jshint": "^1.6.1",
"jshint-stylish": "^0.2.0",
"gulp-karma": "0.0.4", "gulp-karma": "0.0.4",
"gulp-plumber": "~0.6.2",
"gulp-rename": "~1.1.0",
"gulp-uglify": "~0.3.0",
"jshint-stylish": "^0.2.0",
"karma": "^0.12.16", "karma": "^0.12.16",
"karma-jasmine": "~0.2.0", "karma-jasmine": "~0.2.0",
"karma-phantomjs-launcher": "^0.1.4", "karma-phantomjs-launcher": "^0.1.4",

7
src/echo.js

@ -16,7 +16,7 @@
var callback = function () {}; var callback = function () {};
var offset, poll, delay, useDebounce, unload; var offset, poll, delay, useDebounce, unload, container;
var inView = function (element, view) { var inView = function (element, view) {
var box = element.getBoundingClientRect(); var box = element.getBoundingClientRect();
@ -52,6 +52,7 @@
useDebounce = opts.debounce !== false; useDebounce = opts.debounce !== false;
unload = !!opts.unload; unload = !!opts.unload;
callback = opts.callback || callback; callback = opts.callback || callback;
container = opts.container || root;
echo.render(); echo.render();
if (document.addEventListener) { if (document.addEventListener) {
root.addEventListener('scroll', debounceOrThrottle, false); root.addEventListener('scroll', debounceOrThrottle, false);
@ -69,8 +70,8 @@
var view = { var view = {
l: 0 - offset.l, l: 0 - offset.l,
t: 0 - offset.t, t: 0 - offset.t,
b: (root.innerHeight || document.documentElement.clientHeight) + offset.b, b: (container.innerHeight || container.clientHeight) + offset.b,
r: (root.innerWidth || document.documentElement.clientWidth) + offset.r r: (container.innerWidth || container.clientWidth) + offset.r
}; };
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
elem = nodes[i]; elem = nodes[i];

Loading…
Cancel
Save