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

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

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 Relatório

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