diff options
author | Jeffrey Tse <jeffreytse.mail@gmail.com> | 2019-09-11 12:19:34 +0800 |
---|---|---|
committer | Jeffrey Tse <jeffreytse.mail@gmail.com> | 2019-09-11 12:21:17 +0800 |
commit | 61eae5c5f9881cab29712f6e696085baa977e1f9 (patch) | |
tree | bb301b58b94fee5dcefbdfae8f0c8fc13ee5bd21 /_includes/extensions/hashlocate.html | |
parent | cd3a8163dd997d0a7c4d320816651efa8f7dce60 (diff) |
release: v1.0.0
Diffstat (limited to '_includes/extensions/hashlocate.html')
-rw-r--r-- | _includes/extensions/hashlocate.html | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/_includes/extensions/hashlocate.html b/_includes/extensions/hashlocate.html new file mode 100644 index 0000000..d44b249 --- /dev/null +++ b/_includes/extensions/hashlocate.html @@ -0,0 +1,39 @@ +<script> + function hashLocate(hashValue) { + hashValue = hashValue.replace(/^.*#h-/, ''); + var element = document.getElementById(hashValue); + + if (!element) { + return; + } + + var headerHeight = 0; + var header = document.querySelector('header'); + if (header) { + headerHeight = header.offsetHeight; + } + var supportPageOffset = window.pageXOffset !== undefined; + var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"); + + var x = supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft; + var y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop; + + var offsetY = element.offsetTop - headerHeight - 12; + if (y === offsetY) { + return; + } + + window.scrollTo(x, offsetY); + } + + // The first event occurred + if (window.location.hash) { + hashLocate(window.location.hash); + } + + window.addEventListener('click', function(event) { + if (event.target.matches('a')) { + hashLocate(event.target.getAttribute('href')); + } + }); +</script> |