From 664a3a6de71407f6c18a85b0681b8ed8ba99eb43 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Fri, 3 Jun 2016 23:20:56 +0300 Subject: [PATCH] #131:s scroll to anchor considering header height --- src/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 40d47fe..bb6d117 100644 --- a/src/index.js +++ b/src/index.js @@ -53,6 +53,7 @@ userFactory(store) }); +const SCROLL_ANCHOR_OFFSET = 80; // 50 + 30 (header height + some spacing) /** * Scrolls to page's top or #anchor link, if any */ @@ -63,11 +64,16 @@ function restoreScroll() { setTimeout(() => { const id = hash.replace('#', ''); const el = id ? document.getElementById(id) : null; + + let y = 0; if (el) { - el.scrollIntoView(); - } else { - window.scrollTo(0, 0); + const {scrollTop} = document.body; + const {top} = el.getBoundingClientRect(); + + y = scrollTop + top - SCROLL_ANCHOR_OFFSET; } + + window.scrollTo(0, y); }, 100); }