diff options
Diffstat (limited to 'js/global.js')
-rw-r--r-- | js/global.js | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/js/global.js b/js/global.js index 2b353a69e..93f364c9e 100644 --- a/js/global.js +++ b/js/global.js @@ -192,6 +192,16 @@ $().ready(function() { }); /** + * Focus the main content when the page is loaded and there is no autofocus + * element, so the user can immediately scroll down the page using keyboard. + */ +const focus_main_content = () => { + if (!document.querySelector('[autofocus]')) { + document.querySelector('main').focus(); + } +} + +/** * Check if Gravatar images on the page are successfully loaded, and if blocked * (by any content blocker), replace them with the default/fallback image. */ @@ -210,17 +220,16 @@ const detect_blocked_gravatars = () => { */ const scroll_element_into_view = () => { if (location.hash) { - const $header = document.querySelector('#header'); - const $comment = document.querySelector(location.hash); + const $main = document.querySelector('main'); + const $target = document.querySelector(location.hash); - if ($comment) { - window.setTimeout(() => { - window.scrollTo(0, $comment.offsetTop - $header.offsetHeight - 20); - }, 250); + if ($target) { + window.setTimeout(() => $main.scrollTop = $target.offsetTop - 20, 50); } } } +window.addEventListener('DOMContentLoaded', focus_main_content, { once: true }); window.addEventListener('load', detect_blocked_gravatars, { once: true }); window.addEventListener('load', scroll_element_into_view, { once: true }); window.addEventListener('hashchange', scroll_element_into_view); |