-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.mjs
More file actions
56 lines (50 loc) · 1.66 KB
/
index.mjs
File metadata and controls
56 lines (50 loc) · 1.66 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
import ImageSlider from './components/image-slider.mjs';
ImageSlider.register();
const nav = document.querySelector('nav');
const imageSlider = document.querySelector('image-slider');
nav.addEventListener('click', (e) => {
e.preventDefault();
const active = nav.querySelector('.active');
if (active) {
active.classList.remove('active');
}
if (e.target.nodeName === 'A') {
const a = e.target;
const newIndex = a.getAttribute('href').slice(-1);
a.classList.add('active');
imageSlider.setAttribute('index', newIndex);
}
});
function prevImage() {
const active = nav.querySelector('.active');
if (active) {
active.classList.remove('active');
}
const currentIndex = parseInt(imageSlider.getAttribute('index'), 10) || 1;
const numImages = [...imageSlider.querySelectorAll('img')].length;
const newIndex = (currentIndex > 1) ? (currentIndex-1) : numImages;
document.querySelector(`[href="#image${newIndex}"]`).classList.add('active');
imageSlider.setAttribute('index', newIndex);
}
function nextImage() {
const active = nav.querySelector('.active');
if (active) {
active.classList.remove('active');
}
const currentIndex = parseInt(imageSlider.getAttribute('index'), 10) || 1;
const numImages = [...imageSlider.querySelectorAll('img')].length;
const newIndex = (currentIndex < numImages) ? (currentIndex + 1) : 1;
document.querySelector(`[href="#image${newIndex}"]`).classList.add('active');
imageSlider.setAttribute('index', newIndex);
}
window.addEventListener('keyup', (e) => {
if (imageSlider.fading === true) {
return;
}
if (e.keyCode === 37) {
prevImage();
}
if (e.keyCode === 39) {
nextImage();
}
});