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

219
Visualizações
Javascript - Lazy Loading for iFrame (iFrame re-uploads every time I scroll up or down)

Having issues with my Lazy Load code:

HTML

<div class="mapFrame mobileHide" >
     <iframe class="lazy" data-src="{eval var=$config.gisMapUrl}" id="mapIFrame" ></iframe>
</div>
...
<div class="mapFrame mobileHide" >
    <iframe class="lazy"  data-src=="{eval var=$config.polUrl}" ></iframe>
</div>

Javascript

document.addEventListener("DOMContentLoaded", function() {
  var lazyloadiFrame = document.querySelectorAll("iframe.lazy");    
  var lazyloadThrottleTimeout;
  
  function lazyload () {
    if(lazyloadThrottleTimeout) {
      clearTimeout(lazyloadThrottleTimeout);
    }    
    
    lazyloadThrottleTimeout = setTimeout(function() {
        var scrollTop = window.pageYOffset;
        lazyloadiFrame.forEach(function(iframe) {           
            if(iframe.offsetTop < (window.innerHeight + scrollTop)) {
              iframe.src = iframe.dataset.src;            
              iframe.classList.remove('lazy');  
             
            }
        }  );
        
    }, 20);
  }
 
  document.addEventListener("scroll", lazyload);
  
});

It works like a charm when the page loads; and the iframes do not load at all until I scroll to them. The issue is... Once I scroll to the iFrames, and display the iFrames. I use the scroll bar to move up or down, the iFrames' content keeps reloading itself again and again... Each time I move the scroll bar, it reloads. I need to stop once loaded and just display. Basically, once the scroll bar hits an iframe, iframe loads the content, displays the content and I can continue scrolling without it reloading the content.

Reloading takes too much time, and there is also a focus within the iframes content, which causes it to go to that particular iframe whenever you hit the scroll bar, and reloads it again. I need to have just have the iframe to display the content once it is loaded, and I can continue scrolling up or down without reloading it.

I tried removing the class="lazy", and it does remove the class but the content still reloads every time I hit the scrollbar. I tried removing scrollTop, but that just seems to remove the content within an iframe.

I am a bit rusty with javascript coding and anyone's help is appreciated especially in coding what to do next. Please help!

about 4 years ago · Juan Pablo Isaza
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