-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-multi-carousel.html
65 lines (58 loc) · 1.42 KB
/
simple-multi-carousel.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
<!DOCTYPE html>
<html>
<head>
<title>Multi-Image Carousel Example</title>
<style>
#carousel {
width: 800px;
height: 400px;
overflow: hidden;
position: relative;
}
#carousel img {
width: 200px;
height: 400px;
float: left;
}
</style>
</head>
<body>
<div id="carousel">
<img src="image1.jpg" />
<img src="image2.jpg" />
<img src="image3.jpg" />
<img src="image4.jpg" />
<img src="image5.jpg" />
<img src="image6.jpg" />
</div>
<button id="prev-btn">Prev</button>
<button id="next-btn">Next</button>
<script>
const carousel = document.getElementById("carousel");
const prevBtn = document.getElementById("prev-btn");
const nextBtn = document.getElementById("next-btn");
let currentPos = 0;
let numVisible = 3;
function next() {
currentPos = Math.min(
currentPos + numVisible,
carousel.childNodes.length - numVisible
);
updateCarousel();
}
function prev() {
currentPos = Math.max(currentPos - numVisible, 0);
updateCarousel();
}
function updateCarousel() {
carousel.style.left = -currentPos * 200 + "px";
}
nextBtn.onclick = function () {
next();
};
prevBtn.onclick = function () {
prev();
};
</script>
</body>
</html>