Created
June 17, 2021 12:04
-
-
Save Raja0sama/6002a2f9f9eefe1f9d7ac4a7f21e8a68 to your computer and use it in GitHub Desktop.
Load more feature for any HTML element containing multiple childnodes.
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
let init = (el) => { | |
var container = document.getElementsByClassName(el) | |
let list = container[0].children | |
const defEl = 2 | |
let def = defEl | |
Array.from(list).forEach(e => e.style.display = "none") | |
return () => { | |
let a = false | |
Array.from(list).forEach((e, i) => { | |
console.log({ i, def }) | |
if (i < def) { | |
e.style.display = "block" | |
a = true | |
} | |
}) | |
if (def > Array.from(list).length - 1) document.querySelector('.load-more-btn a').style.display = "none" | |
def = def + defEl | |
} | |
} | |
// Init with the parent class | |
const paginate = init('bolgList') | |
paginate() | |
// Loadmore button reference inside the querySelector. | |
document.querySelector('.load-more-btn a').onclick = () => { | |
paginate() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool, loadmore feature.