Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Lazy Loading an array of images fails to update to the next image in the array

I am trying to implement lazy loading on my personal project. I have been able to make it work on my homepage, or my gallery page, but never both.

I believe the issue is that "lowResImage" is not updating it's path to the next item in the array.

I apologize if this code is poorly written, I am just beginning to learn, and I've moved things around a lot trying to find a solution.

// SET RES-IMAGE-REPLACEMENT, AND RUN LAZYLOADERSETUP ON EACH IMAGE
resImageReplacements = document.getElementsByClassName('res-image-replacement');

for (let i = 0; i < resImageReplacements.length; i++) {
  lazyLoaderSetup(resImageReplacements[i], i);
}



function lazyLoaderSetup(i) {
  highResImage = document.createElement("IMG");
  lowResImage = document.getElementsByClassName('picturesToBeSwapped')[i];
  resReplacement = document.getElementsByClassName("res-image-replacement")[i];

  // SET HIGH RES IMAGE UP WITH CLASS, ID, AND HIGH RES SOURCE
  highResImage.setAttribute("class", "mainBackground");
  highResImage.setAttribute("id", "mainBackground");
  highResImage.setAttribute('src', lowResImage.getAttribute("high-res-src"));

  // IF THE LOW RES IMAGE CLASS EXISTS, ADD THE LOAD LISTENER WHICH WILL RUN REMOVEAPPEND FUNCTION
  if (resReplacement.contains(lowResImage)) {
    highResImage.addEventListener('load', removeAppend);
  }
}




// REMOVE THE LOW RES ELEMENT, ADD THE HIGH RES ELEMENT, REMOVE LISTENER
function removeAppend() {
  resReplacement.removeChild(lowResImage);
  resReplacement.appendChild(highResImage);
  highResImage.removeEventListener('load', removeAppend);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I finally got the Lazy Loader to function correctly. Setting lowResImage to always be the first element in the picturesToBeSwapped array fixed my issue. Here is my updated code, hopefully it can be helpful to someone in the future.

// SET RES-IMAGE-REPLACEMENT, AND RUN LAZYLOADERSETUP ON EACH IMAGE
resImageReplacements = document.getElementsByClassName('res-image-replacement');

for (let i = 0; i < resImageReplacements.length; i++) {
     lazyLoaderSetup(i);

}



function lazyLoaderSetup( i) {
  highResImage = document.createElement("IMG");
  lowResImage = document.getElementsByClassName('picturesToBeSwapped')[0];
  resReplacement = document.getElementsByClassName("res-image-replacement")[i];

  // SET HIGH RES IMAGE UP WITH CLASS, ID, AND HIGH RES SOURCE
  highResImage.setAttribute("class", "mainBackground");
  highResImage.setAttribute('src', lowResImage.getAttribute("high-res-src"));

  // IF THE LOW RES IMAGE CLASS EXISTS, ADD THE LOAD LISTENER WHICH WILL RUN REMOVEAPPEND FUNCTION
  if (resReplacement.contains(lowResImage)) {
    highResImage.addEventListener('load', removeAppend);
  }
}




// REMOVE THE LOW RES ELEMENT, ADD THE HIGH RES ELEMENT, REMOVE LISTENER
function removeAppend() {
  resReplacement.removeChild(lowResImage);
  resReplacement.appendChild(highResImage);
  highResImage.removeEventListener('load', removeAppend());

}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda