Browse Source

add one config named "container"(scroll element)

default scroll element is body element, add the "container" config to set scroll element.
pull/109/head
dclovec 9 years ago
parent
commit
b1ad876deb
  1. 37
      src/echo.js

37
src/echo.js

@ -1,14 +1,14 @@
(function (root, factory) { (function (window, factory) {
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
define(function() { define(function() {
return factory(root); return factory();
}); });
} else if (typeof exports === 'object') { } else if (typeof exports === 'object') {
module.exports = factory; module.exports = factory;
} else { } else {
root.echo = factory(root); window.echo = factory();
} }
})(this, function (root) { })(window, function () {
'use strict'; 'use strict';
@ -16,7 +16,7 @@
var callback = function () {}; var callback = function () {};
var offset, poll, delay, useDebounce, unload; var offset, poll, delay, useDebounce, unload, container, scrollListenElement;
var isHidden = function (element) { var isHidden = function (element) {
return (element.offsetParent === null); return (element.offsetParent === null);
@ -50,6 +50,9 @@
var optionToInt = function (opt, fallback) { var optionToInt = function (opt, fallback) {
return parseInt(opt || fallback, 10); return parseInt(opt || fallback, 10);
}; };
var bodyElement=document.body;
container = opts.container || bodyElement;
scrollListenElement = (container!==bodyElement) ? container : window;
offset = { offset = {
t: optionToInt(opts.offsetTop, offsetVertical), t: optionToInt(opts.offsetTop, offsetVertical),
b: optionToInt(opts.offsetBottom, offsetVertical), b: optionToInt(opts.offsetBottom, offsetVertical),
@ -62,11 +65,15 @@
callback = opts.callback || callback; callback = opts.callback || callback;
echo.render(); echo.render();
if (document.addEventListener) { if (document.addEventListener) {
root.addEventListener('scroll', debounceOrThrottle, false); /*root.addEventListener('scroll', debounceOrThrottle, false);
root.addEventListener('load', debounceOrThrottle, false); root.addEventListener('load', debounceOrThrottle, false);*/
scrollListenElement.addEventListener('scroll', debounceOrThrottle, false);
window.addEventListener('load', debounceOrThrottle, false);
} else { } else {
root.attachEvent('onscroll', debounceOrThrottle); /*root.attachEvent('onscroll', debounceOrThrottle);
root.attachEvent('onload', debounceOrThrottle); root.attachEvent('onload', debounceOrThrottle);*/
scrollListenElement.attachEvent('onscroll', debounceOrThrottle);
window.attachEvent('onload', debounceOrThrottle);
} }
}; };
@ -77,8 +84,10 @@
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: (root.innerHeight || document.documentElement.clientHeight) + offset.b,
r: (root.innerWidth || document.documentElement.clientWidth) + offset.r r: (root.innerWidth || document.documentElement.clientWidth) + offset.r*/
b: container.clientHeight + offset.b,
r: container.clientWidth + offset.r
}; };
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
elem = nodes[i]; elem = nodes[i];
@ -122,9 +131,11 @@
echo.detach = function () { echo.detach = function () {
if (document.removeEventListener) { if (document.removeEventListener) {
root.removeEventListener('scroll', debounceOrThrottle); /*root.removeEventListener('scroll', debounceOrThrottle);*/
window.removeEventListener('scroll', debounceOrThrottle);
} else { } else {
root.detachEvent('onscroll', debounceOrThrottle); /*root.detachEvent('onscroll', debounceOrThrottle);*/
window.detachEvent('onscroll', debounceOrThrottle);
} }
clearTimeout(poll); clearTimeout(poll);
}; };

Loading…
Cancel
Save