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

NW-6 | Zeliha Pala | JS2| [TECH ED] Slideshow| WEEK-4 #229

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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions week-3/slideshow/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Slideshow</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 id="carousel-container">
<div class="slider">
<img id="carousel-img" src="./assets/cute-cat-a.png" alt="cat-pic" />
</div>

<div id="btns">
<button type="button" id="auto-forward">Auto Forward</button>
<button type="button" id="backward-btn">Back</button>
<button type="button" id="stop">Stop</button>
<button type="button" id="forward-btn">Forward</button>
<button type="button" id="auto-backward">Auto Back</button>
</div>
</div>
</body>
</html>
95 changes: 91 additions & 4 deletions week-3/slideshow/slideshow.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,95 @@
// slideshow.js

const images = [
"./assets/cute-cat-a.png",
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
"./assets/cute-cat-a.png",
"./assets/cute-cat-b.jpg",
"./assets/cute-cat-c.jpg",
];

let currentIndex = 0;
let intervalId;

const imageElement = document.getElementById("carousel-img");

Choose a reason for hiding this comment

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

good use of "const" and "let" throughout.

const forwardButton = document.getElementById("forward-btn");
const backwardButton = document.getElementById("backward-btn");
const autoForwardButton = document.getElementById("auto-forward");
const autoBackwardButton = document.getElementById("auto-backward");
const stopButton = document.getElementById("stop");
const imgDiv = document.querySelector(".slider");

//added backward and forward point btns- created div for them
const pointDiv = document.createElement("div");
pointDiv.setAttribute("id", "pointDiv");
imgDiv.after(pointDiv);

const forwardPoint = document.createElement("button");
forwardPoint.setAttribute("id", "forwardPoint");
forwardPoint.innerText = ">";
pointDiv.append(forwardPoint);

const backwardPoint = document.createElement("button");
backwardPoint.setAttribute("id", "backwardPoint");
backwardPoint.innerText = "<";
pointDiv.prepend(backwardPoint);

//...........

function updateImage() {
imageElement.src = images[currentIndex];
}

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

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

function startAutoForward() {
intervalId = setInterval(moveForward, 2000);
autoForwardButton.disabled = true;
autoBackwardButton.disabled = true;
}

function startAutoBackward() {
intervalId = setInterval(moveBackward, 2000);
autoForwardButton.disabled = true;
autoBackwardButton.disabled = true;
}

function stopAuto() {
clearInterval(intervalId);
autoForwardButton.disabled = false;
autoBackwardButton.disabled = false;
}

const btns = document.getElementById("btns");
btns.addEventListener("click", function changeImg(event) {
const target = event.target;
if (target.id === "auto-backward") {
startAutoForward();
} else if (target.id === "forward-btn") {
moveForward();
} else if (target.id === "stop") {
stopAuto();
} else if (target.id === "backward-btn") {
moveBackward();
} else if (target.id === "auto-forward") {
startAutoForward();
}
});

backwardPoint.addEventListener("click", moveBackward);
forwardPoint.addEventListener("click", moveForward);

/* forwardButton.addEventListener("click", moveForward);
backwardButton.addEventListener("click", moveBackward);

Choose a reason for hiding this comment

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

small thing, remove commented out code to keep code clean

autoForwardButton.addEventListener("click", startAutoForward);
autoBackwardButton.addEventListener("click", startAutoBackward);
stopButton.addEventListener("click", stopAuto); */

// Write your code here
// Initial image display
window.onload = updateImage;
58 changes: 57 additions & 1 deletion week-3/slideshow/style.css
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
/** Write your CSS in here **/
/* style.css */

body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}

#carousel-container {
max-width: 800px;
margin-top: 50px;
margin-left: auto;
margin-right: auto;
}

.slider {
width: 450px;
height: 300px;
margin: auto;
overflow: hidden;
border: 1px solid #ccc;
box-shadow: 0 0 10px rgba(0, 0, 0, 3.1);
border-radius: 5px;
}

.slider img {
width: 100%;
height: 100%;
object-fit: cover;
}

#btns {
text-align: center;
margin-top: 20px;
}

button {
padding: 10px 15px;
margin: 5px;
cursor: pointer;
background-color: #77797c;
color: #fff;
font-size: 14px;
border: none;
border-radius: 10px;
}

button:hover {
background-color: #85718d;
}

#pointDiv {
display: flex;
justify-content: center;
margin-top: 15px;
}