From 2e5d910d9401c4fa8f105d8f9502d9e4ea27bb99 Mon Sep 17 00:00:00 2001 From: Kohei Yoshino Date: Wed, 13 Dec 2017 16:06:06 -0500 Subject: Bug 1376826 - New HTML Header for BMO --- js/comments.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'js/comments.js') 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); -- cgit v1.2.3-24-g4f1b