-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
102 lines (89 loc) · 2.78 KB
/
index.html
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!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">
<title>Image Slider</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: #d8d8d8;
}
main {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.slider-container {
max-width: 800px;
width: 90%;
margin: auto;
overflow: hidden;
position: relative;
}
.slider {
display: flex;
transition: all 0.5s ease-in-out;
}
.slide {
flex: 0 0 100%;
height: 400px;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
.slider-btns {
display: flex;
justify-content: space-between;
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 100%;
}
.btn {
padding: 0.5rem 1rem;
border: none;
outline: none;
border-radius: 4px;
}
</style>
</head>
<body>
<main>
<div class="slider-container">
<div class="slider">
<div class="slide" style="background-image: url(./img/pexels-eberhard-grossgasteiger-949372.jpg);">
</div>
<div class="slide" style="background-image: url(./img/pexels-jovana-nesic-593655.jpg);"></div>
<div class="slide" style="background-image: url(./img/pexels-susanne-jutzeler-sujufoto-1169084.jpg);">
</div>
</div>
<div class="slider-btns">
<button class="prev-btn btn">Prev</button>
<button class="next-btn btn">Next</button>
</div>
</div>
</main>
<script>
const slider = document.querySelector('.slider');
const slides = document.querySelectorAll('.slide');
const prevBtn = document.querySelector('.prev-btn');
const nextBtn = document.querySelector('.next-btn');
let index = 0;
nextBtn.addEventListener('click', () => {
index = index === slides.length - 1 ? 0 : index + 1;
slider.style.transform = `translateX(-${index * 100}%)`;
})
prevBtn.addEventListener('click', () => {
index = index === 0 ? slides.length - 1 : index - 1;
slider.style.transform = `translateX(-${index * 100}%)`;
})
</script>
</body>
</html>