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" -}}