Video.js
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React, { useRef, useEffect, useState } from "react";
import { Container } from "react-bootstrap";
import { cleanImage } from "../services/imageHandling";
const Video = ({productData}) => {
const videoRef = useRef(null);
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setIsVisible(true);
// 👇 Stop observing after first visibility
observer.unobserve(entry.target);
}
},
{ threshold: 0.5 }
);
if (videoRef.current) observer.observe(videoRef.current);
return () => {
if (videoRef.current) observer.unobserve(videoRef.current);
};
}, []);
return (
<section className="video_sec">
<div className="custom_containers">
<video
ref={videoRef}
autoPlay
muted
loop
playsInline
className={`w-100 video-animate ${isVisible ? "video-visible" : ""}`}
>
<source src={cleanImage(productData?.video?.url)} type="video/mp4" />
Your browser does not support the video tag.
</video>
</div>
</section>
);
};
export default Video;