const prevlink = document.getElementById('prev-link'); const nextlink = document.getElementById('next-link'); const posts = document.getElementsByTagName('article'); function keyUp(e) { var i, post; var el = document.activeElement; if (! (el && (el.tagName.toLowerCase() == 'input' || el.tagName.toLowerCase() == 'textarea'))) { if (! (e.ctrlKey || e.altKey || e.shiftKey || e.metaKey )) { if (e.code === 'ArrowLeft') { if (prevlink) { window.location.href = prevlink.href; } else if (posts) { i = posts.length-1; post = posts[i]; while (i >= 0 && post.offsetTop + 5 > window.scrollY) { post=posts[i--]; } post.scrollIntoView() } } else if (e.code === 'ArrowRight') { if (nextlink) { window.location.href = nextlink.href; } else if (posts) { i = 0; post = posts[i]; while (i < posts.length && post.offsetTop - 5 < window.scrollY) { post=posts[i++]; } post.scrollIntoView() } } } } } document.addEventListener('keyup', keyUp, false);