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

224
Vistas
Intersection Observer Not Working Wordpress 5.8.1

I implemented Intersection Observer on my Wordpress site which is on dev mode atm and just the last image at the bottom of the site is lazyloading.

Not sure the reason why that is happening. See below my lazy code.

 /*Lazy load images*/
 const allViews = document.querySelectorAll("[data-src]");

 function preloadImage(img) {
   const src = img.getAttribute("data-src");
     if (!src) {
      return;
 }
  img.src = src;
 }
 const options = {
  root: null,
  threshold: 0,
  rootMargin: "0px",
 };

const callback = function (entries) {
//console.log(entries);
};

const observer = new IntersectionObserver((entries, observer) => {
  entries.forEach((entry) => {
     if (!entry.isIntersecting) {
       return;
  } else {
  console.log(entry.target);
  preloadImage(entry.target);
  observer.unobserve(entry.target);
     }
   });
 }, options);

 allViews.forEach((image) => {
 observer.observe(image);
 });

On another file, I have a jquery script adding the data-src attribute to all images and adding the lazyload class:

  /*change img src to img data-src for lazy load*/
  $("img").each(function () {
    $(this).attr("data-src", $(this).attr("src"));
    $(this).addClass('lazyload');
    //$(this).removeAttr("src");
    //console.log($(this)[0].outerHTML);
});

Any help will be great.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I'm still confused why are you actually using this method because wordpress already has native loading="lazy" support

  1. Javascript might execute in the order it loaded, If the Jquery code is not loaded before the IntersectionObserver code, Your script become useless.
  2. you have comment out //$(this).removeAttr("src"); which means, src attribute still exist and the image will be loaded normally. You should uncomment it.
  3. Instead of spending time developing a lazyload you could simply leave to wordpress do it natively lazy load, or use Plugins like W3 Total Cache, which has inbuilt support for Lazy Loading.
about 4 years ago · Juan Pablo Isaza Denunciar
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