I have to load several elements like a list with javascript.
I have to fill the viewport if necessary because I need load more elements when the user reach the bottom of my container. for that I need to know how many elements fit in the viewport.
With this code I fill the container
for(let i=0; i<lengthDB; i++){
const card = document.createElement("div")
const heading = document.createElement("h1")
const button = document.createElement("button")
button.textContent = "Click Aqui"
card.setAttribute("class","card")
card.appendChild(heading)
card.appendChild(button)
window.cardsContainer.appendChild(card)
counterSpace += document.querySelectorAll(".card")[i].offsetHeight
if(counterSpace > window.cardsContainer.offsetHeight){
break;
}
}
But that code break when the user resize the window, if the window is smaller and then bigger the functionality will break because the user will never arrive the bottom by scrolling
I've think in reload the page when user resize, but I guess this is a bad idea