Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!