40 lines
1,021 B
HTML
40 lines
1,021 B
HTML
<script>
|
|
function hashLocate(hashValue) {
|
|
hashValue = hashValue.replace(/^.*#h-/, '');
|
|
var element = document.getElementById(hashValue);
|
|
|
|
if (!element) {
|
|
return;
|
|
}
|
|
|
|
var header = document.querySelector('header');
|
|
var scrollPos = getScrollPos();
|
|
var offsetY = element.offsetTop - (header.offsetTop + header.offsetHeight + 20);
|
|
|
|
if (offsetY == scrollPos.y) {
|
|
return;
|
|
}
|
|
|
|
if (header.offsetTop == 0 && offsetY > scrollPos.y) {
|
|
offsetY += header.offsetHeight;
|
|
} else if (header.offsetTop < 0 && offsetY < scrollPos.y) {
|
|
offsetY -= header.offsetHeight;
|
|
}
|
|
|
|
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.matches('a')) {
|
|
hashLocate(event.target.getAttribute('href'));
|
|
}
|
|
});
|
|
</script>
|