From cda49e03018d3dbbe8b9dc3927719471150c1608 Mon Sep 17 00:00:00 2001 From: Jeffrey Tse Date: Tue, 24 Sep 2019 14:10:57 +0800 Subject: update: perfect animation, compatibility issue --- _includes/extensions/hashlocate.html | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to '_includes/extensions') 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')); -- cgit v1.2.3