From 62d4d4112cc808c0d306762ce029704402e3b0fd Mon Sep 17 00:00:00 2001 From: Wen Junhua Date: Tue, 6 Feb 2024 23:16:47 +0800 Subject: [PATCH] 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 dd5ad69d..15f0eed8 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 4f492147..7be938ab 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 }}