forked from Faibk/jsdoc-baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollanchor.js
40 lines (33 loc) · 1.15 KB
/
scrollanchor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* global document, window */
/* eslint strict: [2, "function"]*/
// Function to prevent the top navbar from obscuring the page content.
(function() {
'use strict';
// timeout for scrolling the window
var TIMEOUT = 5;
// top navbar height
var TOP_OFFSET = 50;
function scrollTo(hash) {
var element = document.getElementById(hash.replace(/^#/, ''));
var elementOffset;
var rect;
if (element) {
rect = element.getBoundingClientRect();
elementOffset = rect.top + window.pageYOffset;
setTimeout(function() {
window.scrollTo(0, elementOffset - TOP_OFFSET);
}, TIMEOUT);
}
}
window.addEventListener('load', function() {
var currentHash = window.location.hash;
// if we're loading a URL with an anchor, scroll appropriately
if (currentHash && currentHash !== '#') {
scrollTo(currentHash);
}
// if the user clicks on an in-page anchor tag, scroll appropriately
window.addEventListener('hashchange', function() {
scrollTo(window.location.hash);
});
});
})();