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

153
Views
Lazy load solo carga la primera imagen

necesita ayuda urgentemente! Estoy tratando de agregar una función de carga diferida a mi proyecto usando javascript de vainilla, pero solo carga la primera imagen y otras imágenes permanecen borrosas y no se cargan.

Aquí está el guión:

 // Lazy loading images const imgTargets = document.querySelectorAll('img[data-src]'); const loadImg = function (entries, observer) { const [entry] = entries; if (!entry.isIntersecting) return; // Replace src with data-src entry.target.src = entry.target.dataset.src; entry.target.addEventListener('load', function () { entry.target.classList.remove('lazy-img'); }); observer.unobserve(entry.target); }; const imgObserver = new IntersectionObserver(loadImg, { root: null, threshold: 0, rootMargin: '200px', }); imgTargets.forEach(img => imgObserver.observe(img));

CÓDIGO HTML:

 <div class="popup swiper-slide swiper-slide2 first-non" style="background: transparent;"> <img class="lazy-img fetures__img" src="assets/theme/images/laz-non-legendary-1.png" data-src="assets/theme/images/non-legendary-1.png" /> </div> <div class="popup swiper-slide swiper-slide2 first-non" style="background: transparent;"> <img class="lazy-img fetures__img" src="assets/theme/images/laz-non-legendary-2.png" data-src="assets/theme/images/non-legendary-2.png" /> </div> <div class="popup swiper-slide swiper-slide2 first-non" style="background: transparent;"> <img class="lazy-img fetures__img" src="assets/theme/images/laz-non-legendary-3.png" data-src="assets/theme/images/non-legendary-3.png" /> </div>

Aquí está CSS solo para el efecto de desenfoque:

 .lazy-img { filter: blur(20px); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Origen de la imagen

 entry.target.src = entry.target.dataset.src.replace("w=10&", "w=800&");

Umbral de control basado en la necesidad

 threshold: 0.2

Código de secuencia de comandos

 // Lazy loading images const imgTargets = document.querySelectorAll('img[data-src]'); const loadImg = function (entries, observer) { const [entry] = entries; if (!entry.isIntersecting) return; // Replace src with data-src entry.target.src = entry.target.dataset.src.replace("w=10&", "w=800&"); entry.target.addEventListener('load', function () { entry.target.classList.remove('lazy-img'); }); observer.unobserve(entry.target); }; const imgObserver = new IntersectionObserver(loadImg, { root: null, threshold: 0.2, rootMargin: '200px', }); imgTargets.forEach(img => imgObserver.observe(img));
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!