blog/_includes/extensions/hashlocate.html
jeffreytse 844aea6c74 fix: hash locating of cjk heading isn't working (#44)
When the heading includes CJK (Chinese, Japanese, Korean)
characters, the hash locating isn't working correctly, the page
will not automatically scroll to the corresponding heading.
2021-05-27 11:03:50 +08:00

44 lines
1.2 KiB
HTML

<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>