-
Notifications
You must be signed in to change notification settings - Fork 0
/
carousel.html
192 lines (165 loc) · 4.83 KB
/
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!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>
* {
box-sizing: border-box;
}
body {
font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Inter',
'SF Pro', 'San Francisco Pro', 'Helvetica Neue', Helvetica, 'Segoe UI',
Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', sans-serif;
}
/* Carousel */
.carousel {
max-width: 1000px;
position: relative;
margin: auto;
/* Hide the images by default */
& .slide {
display: none;
/* Caption text */
& .text {
color: #f2f2f2;
font-size: 1rem;
text-transform: uppercase;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* Number text (1/3 etc) */
& .number-text {
color: #f2f2f2;
font-size: 0.875rem;
padding: 8px 12px;
position: absolute;
top: 0;
}
}
/* Next & previous buttons */
& .prev,
& .next {
cursor: pointer;
position: absolute;
top: 50%;
margin-top: -1.8rem;
padding: 1rem;
color: #d946ef;
font-weight: bold;
font-size: 1.5rem;
transition: 0.3s ease;
user-select: none;
background: none;
border: none;
/* On hover, change color */
&:hover {
color: #e879f9;
}
}
/* Position the "next button" to the right */
& .next {
right: 0;
}
}
.dots {
text-align: center;
}
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #f0abfc;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active,
.dot:hover {
background-color: #e879f9;
}
/* Fading animation */
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
from {
opacity: 0.4;
}
to {
opacity: 1;
}
}
</style>
</head>
<body>
<!-- Slideshow container -->
<div class="carousel">
<!-- Full-width images with number and caption text -->
<div class="slide fade">
<div class="number-text">1 / 3</div>
<img src="https://picsum.photos/600/300?random=1" style="width: 100%" />
<div class="text">Caption Text</div>
</div>
<div class="slide fade">
<div class="number-text">2 / 3</div>
<img src="https://picsum.photos/600/300?random=2" style="width: 100%" />
<div class="text">Caption Two</div>
</div>
<div class="slide fade">
<div class="number-text">3 / 3</div>
<img src="https://picsum.photos/600/300?random=3" style="width: 100%" />
<div class="text">Caption Three</div>
</div>
<!-- Next and previous buttons -->
<button class="prev" onclick="plusSlides(-1)">❮</button>
<button class="next" onclick="plusSlides(1)">❯</button>
</div>
<br />
<!-- dots/circles -->
<div class="dots">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
</div>
<script>
let slideIndex = 1
showSlides(slideIndex) // call the function to display the first slide
// on next/previous click
function plusSlides(n) {
showSlides((slideIndex += n))
}
// on dots click
function currentSlide(n) {
showSlides((slideIndex = n))
}
// display the slides
function showSlides(n) {
let i
let slides = document.getElementsByClassName('slide')
let dots = document.getElementsByClassName('dot')
// if slide index > number of slides, reset it to 1
if (n > slides.length) slideIndex = 1
// if slide index < 1, set it to the number of slides
if (n < 1) slideIndex = slides.length
// hide all slides
for (i = 0; i < slides.length; i++) {
slides[i].style.display = 'none'
}
// remove the active class from all dots
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(' active', '')
}
// display the current slide
slides[slideIndex - 1].style.display = 'block'
dots[slideIndex - 1].className += ' active'
}
</script>
</body>
</html>