Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

NW6 | Hadika Malik | Module JS2 | Week 4 | Slideshow #220

Open
wants to merge 1 commit 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
Binary file added week-3/slideshow/assets/cute-cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions week-3/slideshow/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<link rel="stylesheet" href="style.css">
<title>Image carousel</title>
<script defer src="slideshow.js"></script>
</head>
<body>
<h1>Cute cats!</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>
<section class="box">
<button type="button" id="auto-backward-btn">Auto Backwards</button>
<button type="button" id="backward-btn">Backwards</button>
<button type="button" id="stop-btn">Stop</button>
<button type="button" id="forward-btn">Forward</button>
<button type="button" id="auto-forward-btn">Auto Forward</button>
</section>
</body>
</html>
34 changes: 33 additions & 1 deletion week-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,39 @@ const images = [
"./assets/cute-cat-a.png",
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
"./assets/cute-cat.jpg"

Choose a reason for hiding this comment

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

good job on adding the 4th cat!

];


// Write your code here
// Write your code here

const autoBackBtn = document.getElementById("auto-backward-btn");
const backBtn = document.getElementById("backward-btn");
const stopBtn = document.getElementById("stop-btn");
const fwrdBtn = document.getElementById("forward-btn");
const autoFwrdBtn = document.getElementById("auto-forward-btn");
const carouselImages = document.getElementById("carousel-img");
let currentIndex = 1;
let autoIntervalId;

function forward (){
currentIndex = (currentIndex+1) % images.length;
carouselImages.src = images[currentIndex];
}

function backward (){
currentIndex = (currentIndex+3) % images.length;
carouselImages.src = images[currentIndex];
}

backBtn.addEventListener("click", backward);
fwrdBtn.addEventListener("click", forward);
autoBackBtn.addEventListener("click", ()=>{
autoIntervalId = setInterval(backward,2000);
});
autoFwrdBtn.addEventListener("click", ()=>{
autoIntervalId = setInterval(forward,2000);
});
stopBtn.addEventListener("click", ()=>{
clearInterval(autoIntervalId);

Choose a reason for hiding this comment

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

great job with the event listeners and intervals, consider what happens though when the autoForward button is clicked multiple times.

});
35 changes: 35 additions & 0 deletions week-3/slideshow/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
/** Write your CSS in here **/
body{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color:azure;
}

#box{
display: flex;
flex-direction: row;
}

img{
width: 500px;
height: 400px;
margin: 50px 0px;
border: solid black 2px;
border-radius: 5px;
}

button{
border: black solid 2px;
border-radius: 5px;
background-color:lightsteelblue;
padding: 10px;
margin: 0px 5px;
font-size: large;
}

h1{
margin:50px 0px -10px 0px;
color: rgb(10, 72, 153);
font-weight: bold;
}