From cda49e03018d3dbbe8b9dc3927719471150c1608 Mon Sep 17 00:00:00 2001 From: Jeffrey Tse Date: Tue, 24 Sep 2019 14:10:57 +0800 Subject: [PATCH] update: perfect animation, compatibility issue --- _includes/extensions/hashlocate.html | 31 +++++------ _includes/head.html | 3 +- _includes/sidebar/article-menu.html | 81 ++++++++++++++-------------- _includes/views/header.html | 17 ++---- _sass/misc/common-list.scss | 2 - _sass/misc/google-translate.scss | 25 ++++----- _sass/yat/_base.scss | 2 +- _sass/yat/_layout.scss | 3 +- assets/{ => css}/main.scss | 0 assets/js/main.js | 57 ++++++++++++++++++++ 10 files changed, 134 insertions(+), 87 deletions(-) rename assets/{ => css}/main.scss (100%) create mode 100644 assets/js/main.js 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 scrollPos = getScrollPos(); + var offsetY = element.offsetTop - (header.offsetTop + header.offsetHeight + 20); - 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) { + 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')); diff --git a/_includes/head.html b/_includes/head.html index 770a7bf..be3be7a 100644 --- a/_includes/head.html +++ b/_includes/head.html @@ -6,7 +6,8 @@ {%- seo -%} - + + {%- feed_meta -%} {%- if jekyll.environment == 'production' and site.google_analytics -%} {%- include extensions/google-analytics.html -%} diff --git a/_includes/sidebar/article-menu.html b/_includes/sidebar/article-menu.html index aa4184d..7f83160 100644 --- a/_includes/sidebar/article-menu.html +++ b/_includes/sidebar/article-menu.html @@ -12,48 +12,49 @@ diff --git a/_includes/views/header.html b/_includes/views/header.html index 835d4cb..f23401f 100644 --- a/_includes/views/header.html +++ b/_includes/views/header.html @@ -62,21 +62,14 @@