From 62d4d4112cc808c0d306762ce029704402e3b0fd Mon Sep 17 00:00:00 2001 From: Wen Junhua Date: Tue, 6 Feb 2024 23:16:47 +0800 Subject: [PATCH 1/2] feat: add js to check height and decide if to top is required --- assets/js/appearance.js | 15 +++++++++++++++ layouts/_default/baseof.html | 26 ++++++++++++++------------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/assets/js/appearance.js b/assets/js/appearance.js index dd5ad69d1..15f0eed89 100644 --- a/assets/js/appearance.js +++ b/assets/js/appearance.js @@ -38,7 +38,22 @@ if (document.documentElement.getAttribute("data-auto-appearance") === "true") { }); } +function add_to_top_elem() { + var body = document.body, + html = document.documentElement; + + const height = Math.max( + body.scrollHeight, + body.offsetHeight, + html.clientHeight, + html.scrollHeight, + html.offsetHeight + ) - 150; + document.getElementById("to-top").hidden = height < window.innerHeight; +} + window.addEventListener("DOMContentLoaded", (event) => { + add_to_top_elem(); var switchers = document.querySelectorAll("[id^='appearance-switcher']"); switchers.forEach((switcher) => { switcher.addEventListener("click", () => { diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 4f492147e..7be938ab9 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -29,18 +29,20 @@
{{ block "main" . }}{{ end }} - {{ if and (.Site.Params.footer.showScrollToTop | default true) (gt .WordCount 200) }} - - {{ end }} +
{{- partial "footer.html" . -}} {{ if .Site.Params.enableSearch | default false }} From 86416cbe2731a5289659200532f4707e5318899c Mon Sep 17 00:00:00 2001 From: Wen Junhua Date: Tue, 30 Apr 2024 23:07:58 +0800 Subject: [PATCH 2/2] fix: update to also obey site settings --- assets/js/appearance.js | 27 +++++++++++++++++---------- layouts/_default/baseof.html | 6 ++++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/assets/js/appearance.js b/assets/js/appearance.js index 2f01c4ff5..0cdfd1565 100644 --- a/assets/js/appearance.js +++ b/assets/js/appearance.js @@ -39,17 +39,24 @@ if (document.documentElement.getAttribute("data-auto-appearance") === "true") { } function add_to_top_elem() { - var body = document.body, - html = document.documentElement; + var body = document.body; + var html = document.documentElement; - const height = Math.max( - body.scrollHeight, - body.offsetHeight, - html.clientHeight, - html.scrollHeight, - html.offsetHeight - ) - 150; - document.getElementById("to-top").hidden = height < window.innerHeight; + const height = + Math.max( + body.scrollHeight, + body.offsetHeight, + html.clientHeight, + html.scrollHeight, + html.offsetHeight + ) - 150; + + const elem = document.getElementById("to-top"); + if (elem === null || elem === undefined) { + return; + } + + elem.hidden = height < window.innerHeight; } window.addEventListener("DOMContentLoaded", (event) => { diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 02a89bd74..28c8daf91 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -29,10 +29,12 @@
{{ block "main" . }}{{ end }} +
+ {{ if .Site.Params.footer.showScrollToTop | default true }} - + {{ end }} {{- partial "footer.html" . -}} {{ if .Site.Params.enableSearch | default false }} {{- partial "search.html" . -}}