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

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

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 Report

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