-
Notifications
You must be signed in to change notification settings - Fork 5
/
pdp-product-image-with-thumbnails.html
332 lines (276 loc) · 9.77 KB
/
pdp-product-image-with-thumbnails.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>accessible-slick - PDP product images with thumbnails accessible boilerplate</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@accessible360/[email protected]/slick/slick.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/@accessible360/[email protected]/slick/accessible-slick-theme.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 a large PDP image with an accessible carousel of product thumbnails using accessible-slick.">
<meta name="keywords" content="accessible, accessibility, WCAG, carousel, slider, Slick, jQuery, PDP, thumbnails, template, boilerplate">
<!-- Open Graph -->
<meta property="og:title" content="accessible-slick - PDP product images with thumbnails accessible boilerplate">
<meta property="og:description" content="Pre-built code sample for a large PDP image with an accessible carousel of product thumbnails using accessible-slick.">
<meta property="og:type" content="website">
<!-- Twitter Card -->
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="accessible-slick - PDP product images with thumbnails accessible boilerplate">
<meta name="twitter:description" content="Pre-built code sample for a large PDP image with an accessible carousel of product thumbnails using accessible-slick.">
<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>
<div class="product-images">
<!-- Large, currently-active image -->
<div class="current-image">
<a href="https://accessible360.com">
<img src="../images/blanca-paloma-sanchez-500x500.jpg" alt="Succulents 1">
</a>
</div>
<!-- Thumbnail track -->
<div class="thumbnails">
<div class="thumbnail">
<button class="thumbnail-button" aria-current="true">
<img src="../images/blanca-paloma-sanchez-500x500.jpg" alt="image 1 of 5" data-full-alt="Succulents 1">
</button>
</div>
<div class="thumbnail">
<button class="thumbnail-button">
<img src="../images/erol-ahmed-500x500.jpg" alt="image 2 of 5" data-full-alt="Succulents 2">
</button>
</div>
<div class="thumbnail">
<button class="thumbnail-button">
<img src="../images/armando-castillejo-500x500.jpg" alt="image 3 of 5" data-full-alt="Succulents 3">
</button>
</div>
<div class="thumbnail">
<button class="thumbnail-button">
<img src="../images/calle-macarone-500x500.jpg" alt="image 4 of 5" data-full-alt="Succulents 4">
</button>
</div>
<div class="thumbnail">
<button class="thumbnail-button">
<img src="../images/annie-spratt-500x500.jpg" alt="image 5 of 5" data-full-alt="Succulents 5">
</button>
</div>
</div>
</div>
<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://cdn.jsdelivr.net/npm/@accessible360/[email protected]/slick/slick.min.js"></script>
<script>
var currentImage;
var thumbnails, thumbnailButtons;
window.addEventListener('DOMContentLoaded', function(e) {
currentImage = document.querySelector('.current-image');
/**
When Slick is initialized, retrieve the relevant DOM elements and watch for activation of the thumbnail buttons
*/
$('.thumbnails').on('init', function(e, slick) {
thumbnailButtons = document.querySelectorAll('.thumbnails .thumbnail .thumbnail-button');
// Update the large image each time a thumbnail is activated
thumbnailButtons.forEach(function(thumbnailButton) {
thumbnailButton.addEventListener('click', function(e) {
activateThumbnail(thumbnailButton);
});
});
});
/**
Initialize Slick Slider with recommended configuration options
*/
$('.thumbnails').slick({
slidesToShow: 3,
prevArrow: '<button class="previous-button button">' +
' <span class="fas fa-angle-left" aria-hidden="true"></span>' +
' <span class="sr-only">Previous slide</span>' +
'</button>',
nextArrow: '<button class="next-button button">' +
' <span class="fas fa-angle-right" aria-hidden="true"></span>' +
' <span class="sr-only">Next slide</span>' +
'</button>',
infinite: false,
responsive: [
{
breakpoint: 575,
settings: {
slidesToShow: 2
}
},
{
breakpoint: 375,
settings: {
slidesToShow: 1
}
}
]
});
});
/**
Update the large current image when a thumbnail button is activated.
*/
function activateThumbnail(thumbnailButton) {
// Swap the current image based to match the thumbnail
// - If you'd like to use separate images, like higher-res versions, consider using the index to pick an appropriate src string from an array, or storing the URI of the higher-res image in a custom data attribute on the thumbnail.
var newImageSrc = thumbnailButton.querySelector('img').getAttribute('src');
var newImageAlt = thumbnailButton.querySelector('img').getAttribute('data-full-alt');
currentImage.querySelector('img').setAttribute('src', newImageSrc);
currentImage.querySelector('img').setAttribute('alt', newImageAlt);
// Remove aria-current from any previously-activated thumbnail
thumbnailButtons.forEach(function(button) {
button.removeAttribute('aria-current');
});
// Indicate to screen readers which thumbnail is selected using aria-current
thumbnailButton.setAttribute('aria-current', true);
}
</script>
<style>
/**
Setup for code demo only
*/
:root {
--current-image-height: 300px;
--thumbnails-track-height: 90px;
--thumbnail-width: 100px;
}
body {
margin: 10px;
font-family: Arial, Helvetica, sans-serif;
}
/** Container */
.product-images {
max-width: 400px;
}
/** Thumbnails track */
.thumbnails {
display: flex;
}
.slick-list {
flex: 1 1 auto;
}
/** Current image */
.current-image {
margin-bottom: 1px;
}
.current-image a {
display: block;
height: var(--current-image-height);
overflow: hidden;
position: relative;
}
.current-image a:before {
content: '';
position: absolute;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
transition: box-shadow .1s linear;
}
.current-image a:focus {
outline: 0;
}
.current-image a:focus:before {
box-shadow: inset 0 0 0 1px black,
inset 0 0 0 5px white;
}
.current-image img {
display: block;
object-fit: cover;
width: 100%;
height: 100%;
}
/** Previous and next buttons */
.button {
background: none;
border: 0;
cursor: pointer;
font-size: 30px;
color: rgba(0,0,0,.7);
padding: 0;
margin: 0 1px;
flex: 0 0 25px;
transition: all .2s linear;
}
.button:hover {
background-color: rgba(0,0,0,.1);
color: black;
}
.button:focus {
outline: none;
background-color: royalblue;
color: rgba(255,255,255,1);
}
/** Single thumbnail */
.thumbnail {
flex: 0 0 var(--thumbnail-width);
margin-right: 1px;
position: relative;
}
.thumbnail-button {
display: block;
padding: 0;
margin-right: 1px;
height: var(--thumbnails-track-height);
cursor: pointer;
border: 0;
background: none;
background-color: rgba(0,0,0,.4);
transition: opacity .1s linear;
}
.thumbnail-button:before {
content: '';
position: absolute;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
transition: box-shadow .1s linear;
}
.thumbnail-button:focus {
outline: 0;
}
.thumbnail-button:focus:before {
box-shadow: inset 0 0 0 1px black,
inset 0 0 0 4px white;
}
.thumbnail-button:focus img,
.thumbnail-button:hover img,
.thumbnail-button[aria-current="true"] img {
opacity: 1;
filter: grayscale(0);
}
.thumbnail-button img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
opacity: .5;
filter: grayscale(.6);
transition: all .1s linear;
}
/** Credits at bottom */
.credits {
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>