Skip to content

London | 25-ITP-May | Reza Jahanimir | Sprint 3 | Image carousel #630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Sprint-3/slideshow/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Image carousel</title>
<script defer src="slideshow.js"></script>
</head>
<body>

<h1>Image Carousel. Level 1 Demo.</h1>

<img id="carousel-img" src="./assets/cute-cat-a.png" alt="cat-pic" />
<button type="button" id="backward-btn">Backwards</button>
<button type="button" id="forward-btn">Forward</button>

<div class="buttons">
<button type="button" id="auto-backward">Auto Backwards</button>
<button type="button" id="backward-btn">Backwards</button>
<button type="button" id="stop">Stop</button>
<button type="button" id="forward-btn">Forward</button>
<button type="button" id="auto-forward">Auto Forward</button>
</div>
</body>
</html>
88 changes: 87 additions & 1 deletion Sprint-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,90 @@ const images = [
];


// Write your code here
// Write your code here
let currentIndex = 0;
let autoTimer = null;
const image = document.querySelector("#carousel-img");


// Displays the image at the given index from the images array by updating the src of the image element.
function showImage(index) {
image.src = images[index];
}


// Increments the currentIndex by 1 to move forward in the image list.
// If it's the last image, it wraps back to the first using modular arithmetic (%).
function moveImageForward() {
currentIndex = (currentIndex + 1) % images.length;
showImage(currentIndex);
}


// Decrements the currentIndex to go back to the previous image.
// If it's the first image, it wraps around to the last one using modular arithmetic to stay within bounds.
function moveImageBackward() {
currentIndex = (currentIndex - 1 + images.length) % images.length;
showImage(currentIndex);
}


// This function will be used to automatically move forward through the images.
// You can implement a timer or interval here to call moveImageForward() repeatedly.
function autoForward() {
autoTimer = setInterval(moveImageForward, 2000);
disableAutoButtons();
};


// This function will be used to automatically move backward through the images.
// You can implement a timer or interval here to call moveImageBackward() repeatedly.
function autoBackward() {
autoTimer = setInterval(moveImageBackward, 2000);
disableAutoButtons();
};

function stopButton() {
clearInterval(autoTimer);
autoTimer = null;
enableAutoButtons();
};

function disableAutoButtons() {
document.querySelector("#auto-forward").disabled = true;
document.querySelector("#auto-backward").disabled = true;
}

function enableAutoButtons() {
document.querySelector("#auto-forward").disabled = false;
document.querySelector("#auto-backward").disabled = false;
}

function setup() {

document.querySelector("#backward-btn").addEventListener("click", ( ) => {
moveImageBackward()
});

document.querySelector("#stop").addEventListener("click", ( ) => {
stopButton()
});

document.querySelector("#forward-btn").addEventListener("click", ( ) => {
moveImageForward()
});

document.querySelector("#auto-backward").addEventListener("click", ( ) => {
autoBackward()
});

document.querySelector("#auto-forward").addEventListener("click", ( ) => {
autoForward()
});

// Show first image on load
showImage(currentIndex);

};

setup();
26 changes: 26 additions & 0 deletions Sprint-3/slideshow/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
/** Write your CSS in here **/
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 40px;
}

#carousel-img {
width: 400px;
height: 300px;
object-fit: cover;
border: 3px solid #ccc;
margin: 20px 0;
border-radius: 8px;
}

.buttons {
display: flex;
justify-content: center;
gap: 10px;
}

button {
font-size: 16px;
padding: 10px 20px;
cursor: pointer;
}