diff options
Diffstat (limited to 'js/comments.js')
-rw-r--r-- | js/comments.js | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/js/comments.js b/js/comments.js index 12bc00d46..88ba49364 100644 --- a/js/comments.js +++ b/js/comments.js @@ -144,7 +144,7 @@ function goto_add_comments( anchor ){ anchor = (anchor || "add_comment"); // we need this line to expand the comment box document.getElementById('comment').focus(); - setTimeout(function(){ + setTimeout(function(){ document.location.hash = anchor; // firefox doesn't seem to keep focus through the anchor change document.getElementById('comment').focus(); @@ -178,3 +178,23 @@ function getText(element) { } return text; } + +/** + * If the URL contains a comment ID like #c10, scroll down the page to show the + * entire comment below the fixed global header. + */ +function scroll_comment_into_view() { + if (location.hash.match(/^#c\d+$/)) { + var $header = document.querySelector('#header'); + var $comment = document.querySelector(location.hash); + + if ($comment) { + window.setTimeout(function() { + window.scrollTo(0, $comment.offsetTop - $header.offsetHeight - 20); + }, 100); + } + } +} + +window.addEventListener('load', scroll_comment_into_view, { once: true }); +window.addEventListener('hashchange', scroll_comment_into_view); |