Skip to content

LONDON | ITP-MAY-25 | TEWODROS BEKERE | SPRINT-3 | Slide-Show #733

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 2 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
17 changes: 13 additions & 4 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>
<link rel="stylesheet" href="style.css" />
<script defer src="slideshow.js"></script>
</head>
<body>
<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>
<h2 id="h2">Image Carousel Demo</h2>

<img id="carousel-image" src="./assets/cute-cat-a.png" alt="Image carousel" />
</div>
<p id="image-index"> </p>
<button type="button" id="auto-back">Auto Back</button>
<button type="button" id="back">Back</button>
<button type="button" id="stop">Stop</button>
<button type="button" id="forward">Forward</button>
<button type="button" id="auto-forward">Auto Forward</button>
</body>
</html>
61 changes: 60 additions & 1 deletion Sprint-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,65 @@ const images = [
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
];
// Write your code here

let currentIndex = 0;
let intervalId = null;

// Write your code here
const indexDisplay = document.getElementById("image-index");
const imageElement = document.getElementById("carousel-image");

function updateImage() {
imageElement.src = images[currentIndex];
indexDisplay.textContent = `${currentIndex + 1} `;
}

function nextImage() {
currentIndex = (currentIndex + 1) % images.length;
updateImage();
Comment on lines +20 to +21
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's best practice to keep the consistent indentation!

}

function previousImage() {
currentIndex = (currentIndex - 1 + images.length) % images.length;
updateImage();
}

function AutoPlay(direction) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done for creating a function to avoid repetition 👍

stopAutoPlay();

intervalId = setInterval(function () {
if (direction === "forward") {
nextImage();
} else {
previousImage();
}
}, 1000); // Every 1 seconds
}

function stopAutoPlay() {
clearInterval(intervalId);

currentIndex = 0; // Reset index
updateImage();
}

document.getElementById("auto-back").addEventListener("click", function () {
AutoPlay("back");
});

document.getElementById("back").addEventListener("click", function () {
previousImage();
});

document.getElementById("forward").addEventListener("click", function () {
nextImage();
});

document.getElementById("auto-forward").addEventListener("click", function () {
AutoPlay("forward");
});

document.getElementById("stop").addEventListener("click", stopAutoPlay);


updateImage();
51 changes: 50 additions & 1 deletion Sprint-3/slideshow/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
/** Write your CSS in here **/
/** Writ/* Reset default styles */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
background-color: #f4f4f4;
text-align: center;
padding: 2rem;
}

#carousel-image {
max-width: 50%;
max-height: 50%;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
margin-bottom: 1.rem;
margin-top: 2rem;
transition: opacity 0.3s ease-in-out;
}

button {
background-color: #007acc;
color: white;
border: none;
padding: 0.6rem 1.2rem;
font-size: 1rem;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}

.image-index {
font-size: 1.2rem;
margin-left: 1rem;
text-align: left;
color: #333;
}

button:hover {
background-color: #005fa3;
}

button:active {
transform: scale(0.97);
}