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

107
Vistas
Is there is any way to add Lazy Loading for video tag with JavaScript?

At the moment, I am working on a project that requires me to add three videos to the homepage, but loading them all at once will reduce the load time considerably.

Also i want to use <video/> tag instead of using <iframe/> because i want that autoplay functionality. What's the best way to do this in React? Using NextJS and Chakra UI.

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

0

You can use IntersectionObserver and do it as below. For React all you have to do is to add the below code in an useEffect with empty dependency.

const video = document.querySelector("video");

function handleIntersection(entries) {
  entries.map(async (entry) => {
    if (entry.isIntersecting) {
      const res = await fetch("/video.mp4");
      const data = await res.blob();
      video.src = URL.createObjectURL(data);
    }
  });
}

const observer = new IntersectionObserver(handleIntersection);
observer.observe(video);
<video autoplay muted loop playsinline></video>

Also I used a video with a relative path to avoid possible CORS issues.

about 4 years ago · Juan Pablo Isaza Denunciar

0

i found a way to do it using '@react-hook/intersection-observer'

import useIntersectionObserver from '@react-hook/intersection-observer'
import { useRef } from 'react'

const LazyIframe = () => {
  const containerRef = useRef()
  const lockRef = useRef(false)
  const { isIntersecting } = useIntersectionObserver(containerRef)
  if (isIntersecting) {
    lockRef.current = true
  }
  return (
    <div ref={containerRef}>
      {lockRef.current && (
        <video
          src={"add video source here"}
          type="video/mp4"
        ></video>
      )}
    </div>
  )
}


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