@@ -59,27 +59,78 @@ document.addEventListener('DOMContentLoaded', function() {
5959 const prevBtn = document . querySelector ( '.carousel-btn.prev' ) ;
6060 const nextBtn = document . querySelector ( '.carousel-btn.next' ) ;
6161 const cards = document . querySelectorAll ( '.carousel-card' ) ;
62+ const carouselContainer = document . querySelector ( '.carousel-container' ) ;
6263
6364 if ( carouselTrack && cards . length > 0 ) {
6465 let currentIndex = 0 ;
6566 const cardWidth = 350 + 32 ; // card width + gap
6667 const maxIndex = Math . max ( 0 , cards . length - Math . floor ( window . innerWidth / cardWidth ) ) ;
68+ let autoplayInterval ;
69+ let isPaused = false ;
6770
6871 function updateCarousel ( ) {
6972 carouselTrack . style . transform = `translateX(-${ currentIndex * cardWidth } px)` ;
7073 }
7174
75+ function nextSlide ( ) {
76+ if ( currentIndex < maxIndex ) {
77+ currentIndex ++ ;
78+ } else {
79+ currentIndex = 0 ; // Loop back to start
80+ }
81+ updateCarousel ( ) ;
82+ }
83+
84+ function prevSlide ( ) {
85+ if ( currentIndex > 0 ) {
86+ currentIndex -- ;
87+ } else {
88+ currentIndex = maxIndex ; // Loop to end
89+ }
90+ updateCarousel ( ) ;
91+ }
92+
93+ // Autoplay functionality
94+ function startAutoplay ( ) {
95+ autoplayInterval = setInterval ( ( ) => {
96+ if ( ! isPaused ) {
97+ nextSlide ( ) ;
98+ }
99+ } , 3000 ) ; // Change slide every 3 seconds
100+ }
101+
102+ function stopAutoplay ( ) {
103+ clearInterval ( autoplayInterval ) ;
104+ }
105+
106+ // Start autoplay
107+ startAutoplay ( ) ;
108+
109+ // Pause on hover
110+ if ( carouselContainer ) {
111+ carouselContainer . addEventListener ( 'mouseenter' , ( ) => {
112+ isPaused = true ;
113+ } ) ;
114+
115+ carouselContainer . addEventListener ( 'mouseleave' , ( ) => {
116+ isPaused = false ;
117+ } ) ;
118+ }
119+
120+ // Button controls
72121 if ( prevBtn ) {
73122 prevBtn . addEventListener ( 'click' , ( ) => {
74- currentIndex = Math . max ( 0 , currentIndex - 1 ) ;
75- updateCarousel ( ) ;
123+ prevSlide ( ) ;
124+ stopAutoplay ( ) ;
125+ startAutoplay ( ) ; // Reset timer
76126 } ) ;
77127 }
78128
79129 if ( nextBtn ) {
80130 nextBtn . addEventListener ( 'click' , ( ) => {
81- currentIndex = Math . min ( maxIndex , currentIndex + 1 ) ;
82- updateCarousel ( ) ;
131+ nextSlide ( ) ;
132+ stopAutoplay ( ) ;
133+ startAutoplay ( ) ; // Reset timer
83134 } ) ;
84135 }
85136
@@ -90,6 +141,7 @@ document.addEventListener('DOMContentLoaded', function() {
90141 carouselTrack . addEventListener ( 'touchstart' , ( e ) => {
91142 startX = e . touches [ 0 ] . clientX ;
92143 isDragging = true ;
144+ isPaused = true ; // Pause while touching
93145 } ) ;
94146
95147 carouselTrack . addEventListener ( 'touchmove' , ( e ) => {
@@ -99,6 +151,7 @@ document.addEventListener('DOMContentLoaded', function() {
99151 carouselTrack . addEventListener ( 'touchend' , ( e ) => {
100152 if ( ! isDragging ) return ;
101153 isDragging = false ;
154+ isPaused = false ; // Resume after touch
102155
103156 const endX = e . changedTouches [ 0 ] . clientX ;
104157 const diff = startX - endX ;
0 commit comments