Skip to content

Instantly share code, notes, and snippets.

@Raja0sama
Created June 17, 2021 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raja0sama/6002a2f9f9eefe1f9d7ac4a7f21e8a68 to your computer and use it in GitHub Desktop.
Save Raja0sama/6002a2f9f9eefe1f9d7ac4a7f21e8a68 to your computer and use it in GitHub Desktop.
Load more feature for any HTML element containing multiple childnodes.
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()
}
@Raja0sama
Copy link
Author

Cool, loadmore feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment