Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda