@@ -4,5 +4,59 @@ const images = [
44 "./assets/cute-cat-c.jpg" ,
55] ;
66
7+ let currentIndex = 0 ;
8+ let intervalId = null ;
79
8- // Write your code here
10+ function getDelay ( ) {
11+ return parseInt ( document . getElementById ( "delay-input" ) . value , 10 ) ;
12+ }
13+
14+ function updateCounter ( ) {
15+ document . getElementById ( "image-counter" ) . textContent =
16+ `${ currentIndex + 1 } / ${ images . length } ` ;
17+ }
18+
19+ function showImage ( index ) {
20+ document . getElementById ( "carousel-img" ) . src = images [ index ] ;
21+ updateCounter ( ) ;
22+ }
23+
24+ function moveForward ( ) {
25+ currentIndex = ( currentIndex + 1 ) % images . length ;
26+ showImage ( currentIndex ) ;
27+ }
28+
29+ function moveBackward ( ) {
30+ currentIndex = ( currentIndex - 1 + images . length ) % images . length ;
31+ showImage ( currentIndex ) ;
32+ }
33+
34+ function setAutoButtons ( disabled ) {
35+ document . getElementById ( "auto-forward" ) . disabled = disabled ;
36+ document . getElementById ( "auto-backward" ) . disabled = disabled ;
37+ }
38+
39+ document . getElementById ( "forward-btn" ) . addEventListener ( "click" , moveForward ) ;
40+ document . getElementById ( "backward-btn" ) . addEventListener ( "click" , moveBackward ) ;
41+
42+ document . getElementById ( "auto-forward" ) . addEventListener ( "click" , ( ) => {
43+ setAutoButtons ( true ) ;
44+ intervalId = setInterval ( moveForward , getDelay ( ) ) ;
45+ } ) ;
46+
47+ document . getElementById ( "auto-backward" ) . addEventListener ( "click" , ( ) => {
48+ setAutoButtons ( true ) ;
49+ intervalId = setInterval ( moveBackward , getDelay ( ) ) ;
50+ } ) ;
51+
52+ document . getElementById ( "stop" ) . addEventListener ( "click" , ( ) => {
53+ clearInterval ( intervalId ) ;
54+ intervalId = null ;
55+ setAutoButtons ( false ) ;
56+ } ) ;
57+
58+ // Update the delay display label when the slider moves
59+ document . getElementById ( "delay-input" ) . addEventListener ( "input" , ( e ) => {
60+ const seconds = ( e . target . value / 1000 ) . toFixed ( 1 ) ;
61+ document . getElementById ( "delay-display" ) . textContent = `${ seconds } s` ;
62+ } ) ;
0 commit comments