-
Notifications
You must be signed in to change notification settings - Fork 5
/
slideshow.js
45 lines (39 loc) · 1.26 KB
/
slideshow.js
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
const slideshows = document.querySelectorAll('.slideshow');
for (const slideshow of slideshows) {
cycleSlideshow(slideshow);
slideshow.addEventListener('click', (event) => {
if (event.target.tagName !== 'IMG') return;
window.clearInterval(slideshow.timer);
cycleSlideshow(slideshow);
});
slideshow.timer = window.setInterval(() => cycleSlideshow(slideshow), 3000);
}
function cycleSlideshow(slideshow) {
const first = slideshow.firstElementChild;
const current = slideshow.querySelector('[data-active]');
const next = current ? current.nextElementSibling : undefined;
for (const child of slideshow.children) hideSlide(child);
showSlide(next || first);
}
function showSlide(element) {
if (!element) return;
element.setAttribute('data-active', '');
if (!element.src && element.dataset.src) {
element.src = element.dataset.src;
}
const children = element.children;
for (const child of children) {
if (!child.src && child.dataset.src) {
child.src = child.dataset.src;
}
}
}
function hideSlide(element) {
if (!element) return;
element.removeAttribute('data-active');
if (!element.children.length) return;
const children = element.children;
for (const child of children) {
child.removeAttribute('data-active');
}
}