-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNavigation.js
28 lines (25 loc) · 960 Bytes
/
Navigation.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
const mainElement = document.querySelector('main');
const posts = mainElement.getElementsByTagName('article');
document.documentElement.addEventListener('keydown', ({ currentTarget, key, target }) => {
if (target.value !== undefined) return;
const scrollPaddingTop = parseInt(getComputedStyle(currentTarget).getPropertyValue('scroll-padding-top'));
switch (key) {
case 'j': {
const targetNode = [...posts].find(childNode => Math.floor(childNode.getBoundingClientRect().y) > scrollPaddingTop);
targetNode?.scrollIntoView();
targetNode?.focus();
break;
}
case 'k': {
const targetNode = [...posts].reverse().find(childNode => Math.ceil(childNode.getBoundingClientRect().y) < scrollPaddingTop);
targetNode?.scrollIntoView();
targetNode?.focus();
break;
}
case '.': {
window.scrollTo({ top: 0, behavior: 'smooth' });
document.activeElement?.blur();
break;
}
}
});