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

226
Visualizações
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 Respostas
Responde à pergunta

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 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