-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathslider.html
More file actions
64 lines (58 loc) · 1.53 KB
/
slider.html
File metadata and controls
64 lines (58 loc) · 1.53 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.container {
height: 50dvh;
max-width: 1320px;
margin: 0 auto;
display: flex;
justify-content: center;
align-items: center;
}
button {
padding: 10px 20px;
font-size: 20px;
text-transform: capitalize;
margin: 0 20px;
}
#sliderImage {
width: 200px;
height: 300px;
}
</style>
</head>
<body>
<div class="container">
<button id="prevButton">prev</button>
<div id="sliderImage"></div>
<button id="nextButton">next</button>
</div>
<script>
const images = [
"<img src='https://picsum.photos/id/42/200/300' alt='image1'>",
"<img src='https://picsum.photos/id/10/200/300' alt='image2'>",
"<img src='https://picsum.photos/id/12/200/300' alt='image3'>",
]
const sliderImage = document.querySelector("#sliderImage")
const prevButton = document.querySelector("#prevButton").addEventListener("click", prevImage)
const nextButton = document.querySelector("#nextButton").addEventListener("click", nextImage)
let currentIndex = 0
function showImage() {
sliderImage.innerHTML = images[currentIndex]
}
showImage(currentIndex)
function prevImage() {
currentIndex = (currentIndex - 1 + images.length) % images.length
showImage()
}
function nextImage() {
currentIndex = (currentIndex + 1) % images.length
showImage()
}
</script>
</body>
</html>