-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlinked-product-cards.html
496 lines (412 loc) · 14.8 KB
/
linked-product-cards.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flickity - linked product cards accessible boilerplate</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/flickity/2.2.2/flickity.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<meta name="description" content="Pre-built code sample for an accessible carousel of linked product cards using Flickity.">
<meta name="keywords" content="accessible, accessibility, WCAG, carousel, slider, Flickity, jQuery, cards, template, boilerplate">
<!-- Open Graph -->
<meta property="og:title" content="Flickity - linked product cards accessible boilerplate">
<meta property="og:description" content="Pre-built code sample for an accessible carousel of linked product cards using Flickity.">
<meta property="og:type" content="website">
<!-- Twitter Card -->
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Flickity - linked product cards accessible boilerplate">
<meta name="twitter:description" content="Pre-built code sample for an accessible carousel of linked product cards using Flickity.">
<meta name="twitter:creator" content="Accessible_360">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
</head>
<body>
<main>
<section class="carousel" aria-label="carousel with cards">
<p class="sr-only">This is a carousel with product cards. Use the previous and next buttons to navigate.</p>
<!-- Previous button -->
<button class="button is-control previous-button">
<span class="fas fa-angle-left" aria-hidden="true"></span>
<span class="sr-only">Previous cards</span>
</button>
<!-- Slide content -->
<div class="cards">
<div class="card" role="group" aria-label="card 1 of 5">
<a href="https://accessible360.com" target="_blank">
<div class="image">
<img src="../images/galina-n-300x300.jpg" class="product-image" alt="">
</div>
<h2 class="title">First product</h2>
</a>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<div class="price">
<!-- add "now"? -->
<span class="new-price">$9.99</span>
<span class="original-price">
<span class="sr-only">Originally:</span>
$100.00
</span>
</div>
</div>
</div>
<div class="card" role="group" aria-label="card 2 of 5">
<a href="https://accessible360.com" target="_blank">
<div class="image">
<img src="../images/curology-300x300.jpg" class="product-image" alt="">
</div>
<h2 class="title">Second product</h2>
</a>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<div class="price">
<span class="new-price">$9.99</span>
<span class="original-price">
<span class="sr-only">Originally:</span>
$100.00
</span>
</div>
</div>
</div>
<div class="card" role="group" aria-label="card 3 of 5">
<a href="https://accessible360.com" target="_blank">
<div class="image">
<img src="../images/imani-clovis-300x300.jpg" class="product-image" alt="">
</div>
<h2 class="title">Third product</h2>
</a>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<div class="price">
<span class="new-price">$9.99</span>
<span class="original-price">
<span class="sr-only">Originally:</span>
$100.00
</span>
</div>
</div>
</div>
<div class="card" role="group" aria-label="card 4 of 5">
<a href="https://accessible360.com" target="_blank">
<div class="image">
<img src="../images/rachit-tank-300x300.jpg" class="product-image" alt="">
</div>
<h2 class="title">Fourth product</h2>
</a>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<div class="price">
<span class="new-price">$9.99</span>
<span class="original-price">
<span class="sr-only">Originally:</span>
$100.00
</span>
</div>
</div>
</div>
<div class="card" role="group" aria-label="card 5 of 5">
<a href="https://accessible360.com" target="_blank">
<div class="image">
<img src="../images/deanna-alys-300x300.jpg" class="product-image" alt="">
</div>
<h2 class="title">Fifth product</h2>
</a>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<div class="price">
<span class="new-price">$9.99</span>
<span class="original-price">
<span class="sr-only">Originally:</span>
$100.00
</span>
</div>
</div>
</div>
</div>
<!-- Next button -->
<button class="button is-control next-button">
<span class="fas fa-angle-right" aria-hidden="true"></span>
<span class="sr-only">Next cards</span>
</button>
</section>
<a href="https://accessible360.com" class="credits" target="_blank">
Built by <b><u>Accessible360</u></b> <img src="https://assets.codepen.io/3609497/accessible360-logo-black.png" alt="">
</a>
</main>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flickity/2.2.2/flickity.pkgd.min.js"></script>
<script>
var $carousel;
var cardsContainer, cards;
var previousButton, nextButton;
// CSS variables with tile sizing parameters defined at :root
var cardWidth, cardMargin
var leftMostCardIndex = 0;
window.addEventListener('DOMContentLoaded', function(e) {
cardWidth = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--card-width'));
cardMargin = parseInt(getComputedStyle(document.documentElement).getPropertyValue('--card-margin'));
cardsContainer = document.querySelector('.carousel .cards');
previousButton = document.querySelector('.carousel .previous-button');
nextButton = document.querySelector('.carousel .next-button');
/**
Initialize Flickity with the recommend configuration options
*/
$carousel = $('.carousel .cards').flickity({
// Default previous/next buttons are placed after the slides, so they are hard to find. They can't be moved using config options, so we'll create custom controls.
prevNextButtons: false,
// Disable keyboard navigation. Using the Left and Right arrow keys for navigation is not a problem, but this also makes the entire slide track focusable, which is awkward for keyboard-based users.
accessibility: false,
// Never use wrap-around with multiple visible slides. DOM order will not match visual appearance due to absolute positioning of slides.
wrapAround: false,
pageDots: false,
cellAlign: 'left',
on: {
ready: handleReady,
settle: handleSettle
}
});
// Go to the previous slide when the Previous button is activated
previousButton.addEventListener('click', function(e) {
$carousel.flickity('previous', true);
});
// Go to the next slide when the Next button is activated
nextButton.addEventListener('click', function(e) {
$carousel.flickity('next', true);
});
});
/**
When Flickity is initialized, tweak the focusable track, fully hide all non-visible slides, and set up click handlers on all cards to make them act like big links.
*/
function handleReady() {
// Retrieve references to the most up-to-date elements
cards = document.querySelectorAll('.carousel .cards .card');
// Add instructive ARIA label to the focusable track
cardsContainer.setAttribute('aria-label', 'Track of cards. Use arrow keys to navigate.');
// Ensure that all the non-visible slides are fully hidden
hideNonVisibleCards();
// Navigate to a card's intended URL when the card is activated
cards.forEach(function(card) {
card.addEventListener('click', function(e) {
window.open(
card.querySelector('a').getAttribute('href'),
'_blank'
);
});
});
}
/**
After a transition is complete, make sure all non-visible slides are fully hidden
*/
function handleSettle(index) {
leftMostCardIndex = index;
// Ensure that all the non-visible slides are fully hidden
hideNonVisibleCards();
}
/**
Fully hide non-visible slides for all users when they exit the view.
*/
function hideNonVisibleCards() {
// Hide all tiles from screen reader and keyboard users
cards.forEach(function(card) {
card.querySelectorAll('a').forEach(function(focusableElement) {
focusableElement.setAttribute('aria-hidden', true);
focusableElement.setAttribute('tabindex', -1);
});
});
// Figure out how many slides are visible (or partially visible)
var containerWidth = $(cardsContainer).width();
// How many tiles can fit into this container (including partially visible ones)?
// NOTE: this method assumes a constant slide width. Update as needed if you want to support variable slide width.
var numVisibleCards = Math.ceil(containerWidth / (cardWidth + cardMargin));
// Make just the desired number of tiles available for screen reader and keyboard users
for(var i = leftMostCardIndex; i < leftMostCardIndex + numVisibleCards; i++) {
if(i < cards.length) {
cards[i].querySelectorAll('a').forEach(function(focusableElement) {
focusableElement.removeAttribute('aria-hidden');
focusableElement.removeAttribute('tabindex');
});
}
}
}
</script>
<style>
/**
Setup for code demo only
*/
:root {
--card-width: 200px;
--card-margin: 15px;
--carousel-height: 240px;
}
body {
margin: 10px;
font-family: Arial, Helvetica, sans-serif;
}
/**
Container
*/
.carousel {
position: relative;
max-width: 700px;
height: var(--carousel-height);
}
/**
Controls (previous/next buttons)
*/
.button.is-control {
background: 0;
border: 0;
padding: 0 10px;
z-index: 1;
cursor: pointer;
font-size: 40px;
color: rgba(0,0,0,.6);
transition: all .2s linear;
}
.button.is-control:hover {
color: rgba(0,0,0,1);
}
.button.is-control:focus {
outline: none;
background-color: rgba(0,0,0,.8);
color: rgba(255,255,255,1);
border-radius: 5px;
}
.previous-button {
position: absolute;
left: 0;
top: calc(var(--carousel-height) / 2 - 30px);
}
.next-button {
position: absolute;
right: 0;
top: calc(var(--carousel-height) / 2 - 30px);
}
/**
Collection of all tiles
*/
.cards {
overflow: hidden;
padding: 2px;
height: calc(var(--carousel-height) + 2px);
width: calc(100% - 100px);
margin-left: 50px;
}
/**
Single card
*/
.card {
width: var(--card-width);
height: var(--carousel-height);
margin-right: var(--card-margin);
margin-bottom: 10px;
margin-left: 3px;
position: relative;
overflow: hidden;
border-radius: 3px;
border: 1px solid rgba(0,0,0,.2);
text-decoration: none;
color: rgba(0,0,0,.9);
cursor: pointer;
transition: all .1s linear;
}
.card a {
text-decoration: none;
}
.card .image {
height: 100px;
padding: 2px;
overflow: hidden;
}
.card .image img {
width: 100%;
height: 100%;
object-fit: cover;
filter: grayscale(.5);
}
.card .title {
margin: 0;
padding: 10px 10px 5px 10px;
font-size: 16px;
font-weight: bold;
color: #000;
}
.card .content {
padding: 0 10px 5px 10px;
transition: color .1s linear;
}
.card .content p {
margin: 7px 0;
font-size: 14px;
opacity: .8;
}
.card .content .price {
position: absolute;
bottom: 8px;
}
.card .content .price .new-price {
font-weight: bold;
}
.card .content .price .original-price {
margin-left: 5px;
font-size: 14px;
font-style: italic;
opacity: .5;
text-decoration: line-through;
}
/* Hover state = add box shadow, underline the title */
.card:hover {
border-color: rgba(0,0,0,.4);
box-shadow: 0 0 10px 0 rgba(0,0,0,.15);
}
.card:hover .image img,
.card:focus .image img {
filter: grayscale(0);
}
.card:hover .title {
text-decoration: underline;
}
.card a:focus {
outline: none;
}
/* Focus state = turn text blue, add large blue outline */
/* NOTE: :focus-within is not supported in IE11 or Edge as of Jan 2020. To add support, use a polyfill such as https://github.com/matteobad/focus-within-polyfill */
.card:focus-within {
border-color: hsl(204, 86%, 53%);
box-shadow: 0 0 0 2px hsl(204, 86%, 53%);
}
.card:focus-within .title,
.card:focus-within .content {
color: hsl(217, 71%, 53%);
}
.card:focus-within .title {
text-decoration: underline;
}
/**
Utilities
*/
.is-hidden {
display: none;
}
/** Credits at bottom */
.credits {
display: inline-block;
margin-top: 20px;
margin-left: 40px;
padding: 10px;
font-size: 14px;
color: black;
text-decoration: none;
opacity: .7;
}
.credits img {
height: 30px;
margin-left: 5px;
margin-top: -2px;
vertical-align: middle;
}
.credits:hover,
.credits:focus {
opacity: 1;
}
</style>
</body>
</html>