@@ -67,27 +67,61 @@ $(document).ready(function() {
6767 } else {
6868 console . log ( 'Background video not found!' ) ;
6969 }
70- // Handle carousel videos autoplay
70+ // Optimize carousel videos: only play when sufficiently visible (desktop only)
7171 var carouselVideos = document . querySelectorAll ( '.carousel video' ) ;
72- carouselVideos . forEach ( function ( video ) {
73- console . log ( 'Processing video, autoplay:' , autoplay ) ;
74- if ( autoplay ) {
75- video . setAttribute ( 'autoplay' , '' ) ;
76- video . setAttribute ( 'preload' , 'auto' ) ; // Change from 'none' to 'auto' for autoplay to work
77- console . log ( 'Added autoplay to video and set preload to auto' ) ;
78-
79- // Try to play the video programmatically as a fallback
80- video . play ( ) . then ( function ( ) {
81- console . log ( 'Video started playing successfully' ) ;
82- } ) . catch ( function ( error ) {
83- console . log ( 'Autoplay failed:' , error ) ;
72+
73+ if ( ! isMobile ) {
74+ // Desktop behavior: play/pause based on visibility
75+ carouselVideos . forEach ( function ( video ) {
76+ video . muted = true ;
77+ video . removeAttribute ( 'autoplay' ) ;
78+ video . setAttribute ( 'preload' , 'metadata' ) ;
79+ } ) ;
80+
81+ var visibilityObserver = new IntersectionObserver ( function ( entries ) {
82+ entries . forEach ( function ( entry ) {
83+ var video = entry . target ;
84+ var isVisible = entry . isIntersecting && entry . intersectionRatio >= 0.6 ;
85+
86+ if ( isVisible ) {
87+ video . setAttribute ( 'preload' , 'auto' ) ;
88+
89+ var carouselRoot = video . closest ( '.carousel' ) ;
90+ if ( carouselRoot ) {
91+ carouselRoot . querySelectorAll ( 'video' ) . forEach ( function ( sibling ) {
92+ if ( sibling !== video && ! sibling . paused ) {
93+ try { sibling . pause ( ) ; } catch ( e ) { /* no-op */ }
94+ }
95+ } ) ;
96+ }
97+
98+ video . play ( ) . catch ( function ( ) { /* Autoplay might be blocked; ignore */ } ) ;
99+ } else {
100+ try { video . pause ( ) ; } catch ( e ) { /* no-op */ }
101+ }
84102 } ) ;
85- } else {
103+ } , { threshold : [ 0 , 0.25 , 0.5 , 0.6 , 0.75 , 1 ] } ) ;
104+
105+ carouselVideos . forEach ( function ( video ) {
106+ visibilityObserver . observe ( video ) ;
107+ } ) ;
108+
109+ document . addEventListener ( 'visibilitychange' , function ( ) {
110+ if ( document . hidden ) {
111+ document . querySelectorAll ( '.carousel video' ) . forEach ( function ( video ) {
112+ try { video . pause ( ) ; } catch ( e ) { /* no-op */ }
113+ } ) ;
114+ }
115+ } ) ;
116+ } else {
117+ // Mobile behavior: never autoplay, keep minimal preload, ensure paused
118+ carouselVideos . forEach ( function ( video ) {
119+ video . muted = true ;
86120 video . removeAttribute ( 'autoplay' ) ;
87- video . setAttribute ( 'preload' , 'none' ) ; // Keep preload as 'none' when no autoplay
88- console . log ( 'Removed autoplay from video and set preload to none' ) ;
89- }
90- } ) ;
121+ video . setAttribute ( 'preload' , 'none' ) ;
122+ // try { video.pause(); } catch (e) { /* no-op */ }
123+ } ) ;
124+ }
91125
92126 // Loop on each carousel initialized
93127 // for(var i = 0; i < carousels.length; i++) {
0 commit comments