Skip to content

Commit

Permalink
completed background slide
Browse files Browse the repository at this point in the history
  • Loading branch information
tsr-kairi committed Jul 4, 2021
1 parent ead1d65 commit 8e0106d
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Day 18 - Background Slider/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const body = document.body;
const slides = document.querySelectorAll(".slide");
const leftBtn = document.getElementById("left");
const rightBtn = document.getElementById("right");

let activeSlide = 0;

rightBtn.addEventListener("click", () => {
activeSlide++;

if (activeSlide >= slides.length - 1) {
activeSlide = 0;
}
setBgToBody();
setActiveSlide();
});

leftBtn.addEventListener("click", () => {
activeSlide--;

if (activeSlide < 0) {
activeSlide = slides.length - 1;
}
setBgToBody();
setActiveSlide();
});

setBgToBody();

function setBgToBody() {
body.style.backgroundImage = slides[activeSlide].style.backgroundImage;
}

function setActiveSlide() {
slides.forEach((slide) => slide.classList.remove("active"));

slides[activeSlide].classList.add("active");
}
58 changes: 58 additions & 0 deletions Day 18 - Background Slider/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link rel="stylesheet" href="./style.css" />
<title>Background | Slider</title>
</head>
<body>
<div class="slider-container">
<div
class="slide active"
style="
background-image: url('https://images.unsplash.com/photo-1621692359357-cb9d16b12eea?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=667&q=80');
"
></div>

<div
class="slide"
style="
background-image: url('https://images.unsplash.com/photo-1618741612051-26ffe925da83?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=753&q=80');
"
></div>

<div
class="slide"
style="
background-image: url('https://images.unsplash.com/photo-1622396090075-ab6b8396fe9b?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80');
"
></div>

<div
class="slide"
style="
background-image: url('https://images.unsplash.com/photo-1592653700973-29a2a09628ad?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80');
"
></div>

<button class="arrow left-arrow" id="left">
<i class="fas fa-arrow-left"></i>
</button>

<button class="arrow right-arrow" id="right">
<i class="fas fa-arrow-right"></i>
</button>
</div>

<script src="./app.js"></script>
</body>
</html>
78 changes: 78 additions & 0 deletions Day 18 - Background Slider/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
*::after,
*::before {
box-sizing: border-box;
margin: 0;
padding: 0;
}

body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
background-position: center center;
background-size: cover;
transition: 0.4s;
margin: 0;
}

body::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background-color: rgba(0, 0, 0, 0.7);
z-index: -1;
}

.slider-container {
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
height: 70vh;
width: 70vw;
position: relative;
overflow: hidden;
}

.slide {
opacity: 0;
height: 100vh;
width: 100vw;
background-position: center center;
background-size: cover;
position: absolute;
top: -15vh;
left: -15vw;
transition: 0.4s ease;
z-index: 1;
}

.slide.active {
opacity: 1;
}

.arrow {
background-color: transparent;
color: #fff;
position: fixed;
padding: 20px;
font-size: 30px;
border: 2px solid orange;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
}

.arrow:focus {
outline: 0;
}

.left-arrow {
left: calc(15vw - 65px);
}

.right-arrow {
right: calc(15vw - 65px);
}

0 comments on commit 8e0106d

Please sign in to comment.