-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcarousel.html
More file actions
441 lines (355 loc) · 12.6 KB
/
carousel.html
File metadata and controls
441 lines (355 loc) · 12.6 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<!DOCTYPE html>
<html lang="en">
<head>
<title>Carousel</title>
<meta charset='utf-8' />
<meta name="description" content="Test : testing" />
<link href="css/fontawesome-free-5.15.4/css/all.css" rel="stylesheet"> <!--load all styles -->
<style type="text/css">
@import url('https://fonts.googleapis.com/css2?family=Merriweather:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900&display=swap');
:root {
--carousel-height: 400px;
--slide-width: calc(100% / 3);
}
* {
box-sizing: border-box;
}
body {
background-color: #f5f5f5;
padding: 20px;
font-family: 'Merriweather', serif;
}
/** Wrapper **********************/
.carousel {
position: relative;
width: calc(100% - 40px);
max-width: 900px;
height: var(--carousel-height);
margin: 0 auto;
padding: 0 20px;
}
/** Previous/next buttons ********/
.previous,
.next {
display: flex;
justify-content: center;
align-items: center;
padding: 5px 10px;
position: absolute;
top: calc(50% - 16px);
cursor: pointer;
font-size: 32px;
border: 0;
background: none;
}
.previous { left: -30px; }
.next { right: -30px; }
.previous { left: -30px; }
.next { right: -30px; }
/** Slides track ***************/
.slides {
list-style: none;
padding: 0;
margin: 0;
display: flex;
width: 100%;
overflow: hidden;
}
/** Individual slides ***********/
.slide {
flex: 0 0 calc(100% / 3 - 10px);
margin: 0 5px;
padding: 20px;
height: var(--carousel-height);
display: flex;
align-items: center;
font-size: 18px;
line-height: 26px;
background-color: white;
}
@media screen and (max-width: 768px) {
.slide {
font-size: 14px;
line-height: 18px;
align-items: flex-start;
}
}
.slide a {
color: black;
font-weight: bold;
}
/** Slide dots ******************/
.navigation {
position: absolute;
left: 0;
bottom: -40px;
list-style: none;
padding: 0;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
.navigation li {
display: inline-block;
}
.navigation li button {
display: block;
width: 10px;
height: 10px;
margin: 0 5px;
border: 0;
border-radius: 100px;
background-color: rgba(0,0,0,.2);
cursor: pointer;
}
.navigation li button:focus {
outline-offset: 4px;
}
/** Active slide dot */
.navigation li button[aria-current="true"] {
background-color: black;
width: 25px;
height: 15px;
border-radius: 200px;
}
.block {
position: relative;
}
/* Block links */
.block {
position: relative;
}
.block a[href]::after, .block button::after {
content: "";
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.block:hover, .block:focus-within, .default:hover, .default:focus-within {
border: 2px solid blue;
/*border-color: rgba(0,0,255,.5);*/
box-shadow: 0 0 .5em rgba(0,0,0,.25);
}
/* ------------------------------------ */
/* Now with another link in the block thinger */
.block.extracta a[href]::after {
bottom: 3.75em;
}
.block.extracta p a[href]::after {
content: none;
}
.block.extracta p a[href] {
display: block;
text-align: center;
margin: 1em 2em 0 2em;
padding: .5em 1em;
border: .1em solid #00c;
border-radius: .25em;
text-decoration: none;
color: #fff;
background-color: #00c;
}
.block.extracta p a[href]:focus, .block.extracta p a[href]:hover {
color: #00c;
background-color: #fff;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="header"><a href="https://www.bbc.com">BBC</a></div>
<div id="content">
<div class="carousel">
<button class="previous">
<span class="fas fa-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous Card</span>
</button>
<ul class="slides" role="presentation">
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="1 of 9">
<p>Explore creative ways to split the bill or send money with a friendly message, colorful theme, GIFs, and more.
<br><br> <a href="https://www.paypal.com">PayPal</a></p>
</li>
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="2 of 9">
<p>Google LLC is an American multinational technology company that specializes in Internet-related services and products, which include online advertising technologies, a search engine, cloud computing, software, and hardware <br><br><a href="https://www.google.com">Google</a></p>
</li>
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="3 of 9">
<p>Yahoo provides a web portal, search engine Yahoo Search, and related services, including My Yahoo!, Yahoo Mail, Yahoo News, Yahoo Finance, Yahoo Sports and its advertising platform, Yahoo! Native. <br><br> <a href="https://www.yahoo.com">Yahoo</a></p>
</li>
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="4 of 9">
<p>Amazon.com, Inc. is an American multinational technology company which focuses on e-commerce, cloud computing, digital streaming, and artificial intelligence. <br><br> <a href="https://www.amazon.com">Amazon</a></p>
</li>
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="5 of 9">
<p>Flipkart is an Indian e-commerce company, headquartered in Bangalore, Karnataka, India, and incorporated in Singapore as a private limited company. <br><br> <a href="https://www.flipcart.com">Flipcart</a></p>
</li>
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="6 of 9">
<p>In Snapchat pictures and messages are usually only available for a short time before they become inaccessible to their recipients<br><br> <a href="https://www.snapchat.com">Snapchat</a></p>
</li>
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="7 of 9">
<p>Instagram is a free photo and video sharing app available on iPhone and Android. <br><br> <a href="https://www.instagram.com">Instagram</a></p>
</li>
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="8 of 9">
<p> Connect with friends, work colleagues or people they don’t know, online. It allows users to share pictures, music, videos, and articles, as well as their own thoughts and opinions with however many people they like<br><br> <a href="https://www.facebook,com">Facebook</a></p>
</li>
<li class="slide block extracta" role="group" aria-roledescription="card" aria-label="9 of 9">
<p>WhatsApp Messenger, or simply WhatsApp, is an internationally available American freeware, cross-platform centralized instant messaging and voice-over-IP service owned by Meta Platforms. <br> <a href="https://web.whatsapp.com">Whatsapp Web</a></p>
</li>
</ul>
<button class="next">
<span class="fas fa-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next Card</span>
</button>
<ul class="navigation">
<li>
<button aria-current="true">
<span class="sr-only">Go to card 1</span>
</button>
</li>
<li>
<button>
<span class="sr-only">Go to card 2</span>
</button>
</li>
<li>
<button>
<span class="sr-only">Go to card 3</span>
</button>
</li>
<li>
<button>
<span class="sr-only">Go to card 4</span>
</button>
</li>
<li>
<button>
<span class="sr-only">Go to card 5</span>
</button>
</li>
<li>
<button>
<span class="sr-only">Go to card 6</span>
</button>
</li>
<li>
<button>
<span class="sr-only">Go to card 7</span>
</button>
</li>
<li>
<button>
<span class="sr-only">Go to card 8</span>
</button>
</li>
<li>
<button>
<span class="sr-only">Go to card 9</span>
</button>
</li>
</ul>
<div id="announcement" aria-live="polite" aria-atomic="true" class="sr-only"></div>
</div>. <!-- carousel div -->
</div> <!--content-->
</div> <!--wrapper-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" ></script>
<script>
var previousButton, nextButton;
var slidesContainer, slides, slideDots;
var leftMostSlideIndex = 0;
var slideGap = 5;
window.addEventListener('DOMContentLoaded', function(e) {
previousButton = document.querySelector('.previous');
nextButton = document.querySelector('.next');
slidesContainer = document.querySelector('.slides');
slides = slidesContainer.querySelectorAll('.slide');
slideDots = document.querySelectorAll('.navigation li button');
// Set up previous/next button behaviors
previousButton.addEventListener('click', previousSlide);
nextButton.addEventListener('click', nextSlide);
// Ensure that all non-visible slides are impossible to reach.
hideNonVisibleSlides();
// Set up the slide dot behaviors
slideDots.forEach(function(dot) {
dot.addEventListener('click', function(e) {
goToSlide(Array.prototype.slice.call(slideDots).indexOf(e.target));
});
});
});
/** Go to previous slide */
function previousSlide() {
if(leftMostSlideIndex > 0) {
goToSlide(leftMostSlideIndex - 1);
} else {
goToSlide(slides.length - 1);
}
}
/** Go to next slide */
function nextSlide() {
if(leftMostSlideIndex < slides.length - 1) {
goToSlide(leftMostSlideIndex + 1);
} else {
goToSlide(0);
}
}
/** Go to a specific slide */
function goToSlide(nextLeftMostSlideIndex) {
// Smoothly scroll to the requested slide
$(slidesContainer).animate({
scrollLeft: (slidesContainer.offsetWidth / 3) * nextLeftMostSlideIndex
}, {
duration: 200
});
// Unset aria-current attribute from any slide dots that have it
slideDots.forEach(function(dot) {
dot.removeAttribute('aria-current');
dot.removeAttribute('aria-disabled');
});
// Set aria-current attribute on the correct slide dot
slideDots[nextLeftMostSlideIndex].setAttribute('aria-current', true);
slideDots[nextLeftMostSlideIndex].setAttribute('aria-disabled', true); /*aria-disabled is preferable to the HTML disabled attribute because this is a circumstance where screen reader users benefit from the disabled button being included in the page Tab sequence.*/
// Update the record of the left-most slide
leftMostSlideIndex = nextLeftMostSlideIndex;
// Update each slide so that the ones that are now off-screen are fully hidden.
hideNonVisibleSlides();
// console.log('hi', nextLeftMostSlideIndex +1 , slides.length)
document.querySelector('#announcement').textContent = 'Showing card ' + (nextLeftMostSlideIndex + 1) + ' of ' + slides.length;
}
/**
Fully hide non-visible slides by adding aria-hidden="true" and tabindex="-1" when they go out of view
*/
function hideNonVisibleSlides() {
// Start by hiding all the slides and their content
slides.forEach(function(slide) {
slide.setAttribute('aria-hidden', true);
slide.querySelectorAll('a, button, select, input, textarea, [tabindex="0"]').forEach(function(focusableElement) {
focusableElement.setAttribute('tabindex', -1);
});
});
// Since we know 3 slides are visible at a time, make sure that the 3 slides starting with the left-most one are not hidden to anybody.
if(leftMostSlideIndex < 6) {
for(var i = leftMostSlideIndex; i < leftMostSlideIndex + 3; i++) {
slides[i].removeAttribute('aria-hidden');
slides[i].querySelectorAll('a, button, select, input, textarea, [tabindex="0"]').forEach(function(focusableElement) {
focusableElement.removeAttribute('tabindex');
});
}
// Since scrolling stops when the carousel reaches the last three slides, we should make sure that these last three slides stay visible until the user wraps or goes backwards.
} else {
for(var i = 6; i < 9; i++) {
slides[i].removeAttribute('aria-hidden');
slides[i].querySelectorAll('a, button, select, input, textarea, [tabindex="0"]').forEach(function(focusableElement) {
focusableElement.removeAttribute('tabindex');
});
}
}
}
</script>
<!--
Block product cards
https://codepen.io/aardrian/pen/vYOGpXb. (Article: https://adrianroselli.com/2020/02/block-links-cards-clickable-regions-etc.html)
-->
</body>
</html>