File tree Expand file tree Collapse file tree 1 file changed +21
-4
lines changed
Expand file tree Collapse file tree 1 file changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -6,11 +6,28 @@ import { useEffect, useRef } from "react";
66export 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 >
You can’t perform that action at this time.
0 commit comments