forked from solygambas/html-css-javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
31 lines (25 loc) · 804 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const body = document.body;
const slides = document.querySelectorAll(".slide");
const leftButton = document.getElementById("left");
const rightButton = document.getElementById("right");
let activeSlide = 0;
const setBackground = () => {
body.style.backgroundImage = slides[activeSlide].style.backgroundImage;
};
const setActiveSlide = () => {
slides.forEach((slide) => slide.classList.remove("active"));
slides[activeSlide].classList.add("active");
};
rightButton.addEventListener("click", () => {
activeSlide++;
if (activeSlide > slides.length - 1) activeSlide = 0;
setBackground();
setActiveSlide();
});
leftButton.addEventListener("click", () => {
activeSlide--;
if (activeSlide < 0) activeSlide = slides.length - 1;
setBackground();
setActiveSlide();
});
setBackground();