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

164
Views
Intersection Observer no está trabajando en elementos que tengan menos del 50 % de visibilidad en la ventana gráfica

He agregado un Intersection Observer para la carga diferida de imágenes. Pero por alguna razón, si un elemento div está presente en la ventana gráfica con menos del 50% de visibilidad, Intersection Observer no está cargando la imagen.

Quiero cargar la imagen incluso cuando está presente del 0% al 100% en la ventana gráfica. Aquí está mi instancia de IntersectionObserver:

 import {useEffect} from "react"; let listenerCallbacks = new WeakMap(); let observer; function handleIntersections(entries) { entries.forEach(entry => { if (listenerCallbacks.has(entry.target)) { let cb = listenerCallbacks.get(entry.target); if (entry.isIntersecting || entry.intersectionRatio > 0) { observer.unobserve(entry.target); listenerCallbacks.delete(entry.target); cb(); } } }); } function getIntersectionObserver() { if (observer === undefined) { observer = new IntersectionObserver(handleIntersections, { root: null, rootMargin: "0px", threshold: [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] }); } return observer; } export function useIntersection(elem, callback) { useEffect(() => { let target = elem.current; let observer = getIntersectionObserver(); listenerCallbacks.set(target, callback); observer.observe(target); return () => { listenerCallbacks.delete(target); observer.unobserve(target); }; }, []); }

resultado actual:

Fragmento de resultado ---

Lo que quiero:

lo que quiero ---

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tener threshold: [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] ejecutaría la devolución de llamada cada vez que la visibilidad supera otro 10%, por lo que estaría extrayendo.

Dado que desea obtener tan pronto como sea visible un poco del div , establezca el threshold en 0, como threshold:0 , y cambie su declaración if dentro de handleIntersections a:

 if (entry.isIntersecting)

Por último, también está relacionado con qué tan rápido se desplaza y qué tan rápido responde su back-end, y qué tan grande es la imagen.

Referencia en mdn .

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!