From 052ef46de5628276a2469ffc2ad50b3e4efa5f5a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 24 Jun 2021 00:21:16 +0200 Subject: [PATCH] don't add unnecessary anchors if we force to the anchor anyway --- index.html | 1 - scripts/blog.js | 7 ++++++- scripts/util.js | 11 ++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 8eeedaf..c77556e 100644 --- a/index.html +++ b/index.html @@ -164,7 +164,6 @@ -

Blog

diff --git a/scripts/blog.js b/scripts/blog.js index 3651bdc..d612f64 100644 --- a/scripts/blog.js +++ b/scripts/blog.js @@ -11,7 +11,12 @@ $.ajax({ success: function (json) { let category = window.location.hash.match(/#blog-(.+)/); populateBlog(json, category && decodeURI(category[1]) || "featured"); - forceToAnchor(); + + let anchor = window.location.hash; + // blog categories should also move to the blog + if (anchor.startsWith("#blog-")) + anchor = anchor.substring(0, 5); + forceToAnchor(anchor); } }); diff --git a/scripts/util.js b/scripts/util.js index 1b72a2e..f6ceab4 100644 --- a/scripts/util.js +++ b/scripts/util.js @@ -9,13 +9,14 @@ function setCookie(key, value, days) { document.cookie = `${key}=${value}; expires=${date.toUTCString()}; path=/`; } -function forceToAnchor() { +function forceToAnchor(customAnchor) { + let anchor = customAnchor || window.location.hash; // this is probably a terrible hack - if (window.location.hash.startsWith("#")) { - let anchor = $(window.location.hash); - if (anchor.length) { + if (anchor.startsWith("#")) { + let element = $(anchor); + if (element.length) { $('html, body').animate({ - scrollTop: anchor.offset().top + scrollTop: element.offset().top }, 0); } }