forked from aymen-ghemam/omc-links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
30 lines (27 loc) · 824 Bytes
/
script.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
const links = document.querySelectorAll('a');
let focusedIndex = 0;
links[focusedIndex].focus();
document.addEventListener('keydown', function(event) {
if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
focusedIndex = (focusedIndex + 1) % links.length;
updateFocus();
event.preventDefault();
} else if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
focusedIndex = (focusedIndex - 1 + links.length) % links.length;
updateFocus();
event.preventDefault();
} else if (event.key === 'Enter') {
links[focusedIndex].click();
}
});
function updateFocus() {
links.forEach((link, index) => {
if (index === focusedIndex) {
link.focus();
// link.classList.add('active');
} else {
// link.classList.remove('active');
link.blur();
}
});
}