11const images = [
2- "./assets/cute-cat-a.png" ,
3- "./assets/cute-cat-b.jpg" ,
4- "./assets/cute-cat-c.jpg" ,
2+ "./assets/cute-cat-a.png" ,
3+ "./assets/cute-cat-b.jpg" ,
4+ "./assets/cute-cat-c.jpg" ,
55] ;
66
7+ //Write your code here
8+ let currentIndex = 0 ;
9+ let intervalId = null ;
710
8- // Write your code here
11+ const imgElement = document . getElementById ( "carousel-img" ) ;
12+ const forwardBtn = document . getElementById ( "forward-btn" ) ;
13+ const backwardBtn = document . getElementById ( "backward-btn" ) ;
14+
15+ const autoForwardBtn = document . getElementById ( "auto-forward" ) ;
16+ const autoBackwardBtn = document . getElementById ( "auto-backward" ) ;
17+ const stopBtn = document . getElementById ( "stop" ) ;
18+
19+ function updateImage ( ) {
20+ imgElement . src = images [ currentIndex ] ;
21+ }
22+
23+ function moveForward ( ) {
24+ currentIndex = ( currentIndex + 1 ) % images . length ;
25+ updateImage ( ) ;
26+ }
27+
28+ function moveBackward ( ) {
29+ currentIndex = ( currentIndex - 1 + images . length ) % images . length ;
30+ updateImage ( ) ;
31+ }
32+
33+ forwardBtn . addEventListener ( "click" , moveForward ) ;
34+ backwardBtn . addEventListener ( "click" , moveBackward ) ;
35+
36+ // ---- AUTOMATIC SLIDESHOW ----
37+
38+ function startAuto ( direction ) {
39+ stopAuto ( ) ; // clear any previous interval
40+
41+ // Disable auto buttons
42+ autoForwardBtn . disabled = true ;
43+ autoBackwardBtn . disabled = true ;
44+
45+ intervalId = setInterval ( ( ) => {
46+ direction === "forward" ? moveForward ( ) : moveBackward ( ) ;
47+ } , 2000 ) ;
48+ }
49+
50+ function stopAuto ( ) {
51+ clearInterval ( intervalId ) ;
52+ intervalId = null ;
53+
54+ // Re-enable buttons
55+ autoForwardBtn . disabled = false ;
56+ autoBackwardBtn . disabled = false ;
57+ }
58+
59+ autoForwardBtn . addEventListener ( "click" , ( ) => startAuto ( "forward" ) ) ;
60+ autoBackwardBtn . addEventListener ( "click" , ( ) => startAuto ( "backward" ) ) ;
61+ stopBtn . addEventListener ( "click" , stopAuto ) ;
0 commit comments