Skip to content

Commit

Permalink
Add infinite params to add infinitely elements
Browse files Browse the repository at this point in the history
  • Loading branch information
luctst committed Nov 6, 2021
1 parent 52feee5 commit 669194e
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
* Create a new lazy HTMLElement everytime you scroll on the last element.
* @param {HTMLElement} container - The container where the HTML Element must be create.
* @param {Array} ops - An array of object this will be the element that Leo will create on scroll.
* @param {Object} [options] - Object of options
* @param {Boolean} [options.infinite=false] - If true Leo will infinite and append all elements in ops when last child in container is scroll, default false
* @param {String} [options.scroll=horizontal] - Define the position to scroll by default it is horizontal can be vertical or horizontal.
*/
module.exports = class Leo {
constructor(container, ops) {
constructor(container, ops, options) {
if (typeof container !== "string") {
throw new Error("Container must be a string");
}
Expand All @@ -17,14 +20,32 @@ module.exports = class Leo {
throw new Error("Leo only accept array");
}

if (options) {
if (typeof options !== 'object') throw new Error('options params must be object');
if (options.infinite) {
if (typeof options.infinite !== 'boolean') throw new Error('infinite params must be Boolean');
}
}

ops.forEach((element) => {
if (typeof element !== "object") {
throw new Error("Options only accepts objects");
}
});

const opss = {
scroll: 'horizontal',
infinite: false
};

this.data = [...ops];
this.container = container;
this.options = options ? { ...opss, ...options } : { ...opss };

if (this.options.scroll === 'horizontal') {
document.querySelector(this.container).style = 'overflow-x:scroll;';
}

this.addSection();
}

Expand All @@ -34,7 +55,14 @@ module.exports = class Leo {
window.addEventListener(
"scroll",
function t() {
if (i > this.data.length) return removeEventListener("scroll", t);
if (i > this.data.length - 1) {
if (this.options.infinite) {
i = 0;
return;
};

return removeEventListener("scroll", t)
};

if (
window.scrollY >=
Expand Down

0 comments on commit 669194e

Please sign in to comment.