blob: 0f3e29e6f8b47a2345fe9786e93b23bb80cd070f (
plain)
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
41
42
43
44
|
<script>
function hashLocate(hashValue) {
hashValue = hashValue.replace(/^.*#h-/, '');
hashValue = decodeURIComponent(hashValue);
var element = document.getElementById(hashValue);
if (!element) {
return;
}
var header = document.querySelector('header.site-header');
var headerRect = header.getBoundingClientRect();
var headerTop = Math.floor(headerRect.top);
var headerHeight = Math.floor(headerRect.height);
var scrollPos = getScrollPos();
var offsetY = element.offsetTop - (headerTop + headerHeight + 20);
if (offsetY == scrollPos.y) {
return;
}
if (headerTop == 0 && offsetY > scrollPos.y) {
offsetY += headerHeight + 2;
} else if (headerTop < 0 && offsetY < scrollPos.y) {
offsetY -= headerHeight - 2;
}
smoothScrollTo(offsetY);
}
// The first event occurred
window.addEventListener('load', function(event) {
if (window.location.hash) {
hashLocate(window.location.hash);
}
});
// The first event occurred
window.addEventListener('click', function(event) {
if (event.target.tagName.toLowerCase() == 'a') {
hashLocate(event.target.getAttribute('href'));
}
});
</script>
|