Skip to content

Commit f19c357

Browse files
committed
let's try this
1 parent 009b295 commit f19c357

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

app/page.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,28 @@ import { useEffect, useRef } from "react";
66
export default function Page() {
77
const videoRef = useRef<HTMLVideoElement>(null);
88

9-
function unmuteVideo() {
9+
useEffect(() => {
10+
const observer = new IntersectionObserver(
11+
(entries) => {
12+
if (videoRef.current) {
13+
if (entries[0].isIntersecting) {
14+
videoRef.current.play()
15+
videoRef.current.muted = false
16+
} else {
17+
videoRef.current.pause()
18+
videoRef.current.muted = true
19+
}
20+
}
21+
},
22+
{ threshold: 0.7 } // 50% visible triggers play/pause
23+
);
24+
1025
if (videoRef.current) {
11-
videoRef.current.muted = false;
26+
observer.observe(videoRef.current);
1227
}
13-
}
28+
29+
return () => observer.disconnect();
30+
}, []);
1431

1532
return (
1633
<>
@@ -22,7 +39,7 @@ export default function Page() {
2239
width={500}
2340
height={500} />
2441
<div>
25-
<video ref={videoRef} onClick={unmuteVideo} className="cursor-pointer" loop autoPlay muted height="500" width="500">
42+
<video ref={videoRef} loop autoPlay muted playsInline height="500" width="500">
2643
<source src="/never-gonna-give-up.mp4" type="video/mp4" />
2744
</video>
2845
</div>

0 commit comments

Comments
 (0)