Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add js to check height and decide if to top is required #806

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions assets/js/appearance.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,29 @@ if (document.documentElement.getAttribute("data-auto-appearance") === "true") {
});
}

function add_to_top_elem() {
var body = document.body;
var html = document.documentElement;

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) => {
add_to_top_elem();
setThemeColor();
var switchers = document.querySelectorAll("[id^='appearance-switcher']");
switchers.forEach((switcher) => {
Expand Down
28 changes: 16 additions & 12 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@
<div class="relative flex grow flex-col">
<main id="main-content" class="grow">
{{ block "main" . }}{{ end }}
{{ if and (.Site.Params.footer.showScrollToTop | default true) (gt .WordCount 200) }}
<div class="pointer-events-none absolute bottom-0 end-0 top-[100vh] w-12">
<a
href="#the-top"
class="pointer-events-auto sticky top-[calc(100vh-5.5rem)] flex h-12 w-12 items-center justify-center rounded-full bg-neutral/50 text-xl text-neutral-700 backdrop-blur hover:text-primary-600 dark:bg-neutral-800/50 dark:text-neutral dark:hover:text-primary-400"
aria-label="{{ i18n "nav.scroll_to_top_title" }}"
title="{{ i18n "nav.scroll_to_top_title" }}"
>
&uarr;
</a>
</div>
{{ end }}
</main>
{{ if .Site.Params.footer.showScrollToTop | default true }}
<div
class="pointer-events-none absolute bottom-0 end-0 top-[100vh] w-12"
id="to-top"
hidden="{{ .Site.Params.footer.showScrollToTop | default true -}}"
>
<a
href="#the-top"
class="pointer-events-auto sticky top-[calc(100vh-5.5rem)] flex h-12 w-12 items-center justify-center rounded-full bg-neutral/50 text-xl text-neutral-700 backdrop-blur hover:text-primary-600 dark:bg-neutral-800/50 dark:text-neutral dark:hover:text-primary-400"
aria-label="{{ i18n "nav.scroll_to_top_title" }}"
title="{{ i18n "nav.scroll_to_top_title" }}"
>
&uarr;
</a>
</div>
{{ end }}
{{- partial "footer.html" . -}}
{{ if .Site.Params.enableSearch | default false }}
{{- partial "search.html" . -}}
Expand Down
Loading