From d1bed05843b7ac2919a704c3efa69d97da676c69 Mon Sep 17 00:00:00 2001 From: Remy Sheppard Date: Sun, 1 Oct 2023 05:06:28 -0400 Subject: [PATCH] feat: Back To Top (#105) * Scroll to top * Update scripts.html --------- Co-authored-by: Xin --- assets/js/back-to-top.js | 18 ++++++++++++++++++ i18n/en.yaml | 2 ++ layouts/partials/scripts.html | 3 ++- layouts/partials/toc.html | 10 ++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 assets/js/back-to-top.js diff --git a/assets/js/back-to-top.js b/assets/js/back-to-top.js new file mode 100644 index 00000000..c7996e74 --- /dev/null +++ b/assets/js/back-to-top.js @@ -0,0 +1,18 @@ +const backToTop = document.querySelector("#backToTop"); + +document.addEventListener("scroll", (event) => { + if (window.scrollY > 300) { + backToTop.classList.remove("opacity-0"); + } else { + backToTop.classList.add("opacity-0"); + } +}); + + +function scrollUp() { + window.scroll({ + top: 0, + left: 0, + behavior: 'smooth' + }); +} diff --git a/i18n/en.yaml b/i18n/en.yaml index df31eafc..d4d7d62a 100644 --- a/i18n/en.yaml +++ b/i18n/en.yaml @@ -2,4 +2,6 @@ onThisPage: "On this page" editThisPage: "Edit this page on GitHub →" lastUpdated: "Last updated on" +backToTop: "Scroll to top" + copyright: "© 2023 Hextra Project." diff --git a/layouts/partials/scripts.html b/layouts/partials/scripts.html index 28cc7939..1d567b11 100644 --- a/layouts/partials/scripts.html +++ b/layouts/partials/scripts.html @@ -5,8 +5,9 @@ {{- $jsCodeCopy := resources.Get "js/code-copy.js" -}} {{- $jsFileTree := resources.Get "js/filetree.js" -}} {{- $jsSidebar := resources.Get "js/sidebar.js" -}} +{{- $jsBackToTop := resources.Get "js/back-to-top.js" -}} -{{- $scripts := slice $jsTheme $jsMenu $jsCodeCopy $jsTabs $jsLang $jsFileTree $jsSidebar | resources.Concat "js/main.js" -}} +{{- $scripts := slice $jsTheme $jsMenu $jsCodeCopy $jsTabs $jsLang $jsFileTree $jsSidebar $jsBackToTop | resources.Concat "js/main.js" -}} {{- if hugo.IsProduction -}} {{- $scripts = $scripts | minify | fingerprint -}} {{- end -}} diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html index bb3759dd..f7955671 100644 --- a/layouts/partials/toc.html +++ b/layouts/partials/toc.html @@ -3,6 +3,7 @@ {{- $toc := .Params.toc | default true -}} {{- $onThisPage := (T "onThisPage") | default "On this page"}} {{- $editThisPage := (T "editThisPage") | default "Edit this page"}} +{{- $backToTop := (T "backToTop") | default "Scroll to top" -}}