diff options
author | Jeffrey Tse <jeffreytse.mail@gmail.com> | 2019-09-24 14:10:57 +0800 |
---|---|---|
committer | Jeffrey Tse <jeffreytse.mail@gmail.com> | 2019-09-24 14:10:57 +0800 |
commit | cda49e03018d3dbbe8b9dc3927719471150c1608 (patch) | |
tree | a94523d13898c106c90e4d9285cf6136d45b7a33 /_includes/extensions/hashlocate.html | |
parent | 92840887d2591d8ba9b92074e193ef1a922c118d (diff) |
update: perfect animation, compatibility issue
Diffstat (limited to '_includes/extensions/hashlocate.html')
-rw-r--r-- | _includes/extensions/hashlocate.html | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/_includes/extensions/hashlocate.html b/_includes/extensions/hashlocate.html index d44b249..6faf553 100644 --- a/_includes/extensions/hashlocate.html +++ b/_includes/extensions/hashlocate.html @@ -7,30 +7,31 @@ 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 scrollPos = getScrollPos(); + var offsetY = element.offsetTop - (header.offsetTop + header.offsetHeight + 20); - var offsetY = element.offsetTop - headerHeight - 12; - if (y === offsetY) { + if (offsetY == scrollPos.y) { return; } - window.scrollTo(x, offsetY); + 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 - if (window.location.hash) { - hashLocate(window.location.hash); - } + 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')); |