Allow disabling ScrollToTop button

- refactor corresponding code

usage =>
in site config:
params:
    disableScrollToTop: true

closes: #321
This commit is contained in:
Aditya Telange 2021-03-25 11:00:22 +05:30
parent a2b3be0169
commit ddf004b709
No known key found for this signature in database
GPG Key ID: 82E844EF3DA99E77

View File

@ -9,6 +9,7 @@
<span>&middot;</span> <span>&middot;</span>
<span>Theme <a href="https://git.io/hugopapermod" rel="noopener" target="_blank">PaperMod</a></span> <span>Theme <a href="https://git.io/hugopapermod" rel="noopener" target="_blank">PaperMod</a></span>
</footer> </footer>
{{- if (not .Site.Params.disableScrollToTop) }}
<a href="#top" aria-label="go to top" title="Go to Top (Alt + G)"> <a href="#top" aria-label="go to top" title="Go to Top (Alt + G)">
<button class="top-link" id="top-link" type="button" accesskey="g"> <button class="top-link" id="top-link" type="button" accesskey="g">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 6" fill="currentColor"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 12 6" fill="currentColor">
@ -16,6 +17,7 @@
</svg> </svg>
</button> </button>
</a> </a>
{{- end }}
{{- partial "extend_footer.html" . -}} {{- partial "extend_footer.html" . -}}
{{- $isHLJSdisabled := (.Site.Params.assets.disableHLJS | default .Params.disableHLJS ) }} {{- $isHLJSdisabled := (.Site.Params.assets.disableHLJS | default .Params.disableHLJS ) }}
{{- if (and (eq .Kind "page") (ne .Layout "archives") (ne .Layout "search") (not $isHLJSdisabled)) }} {{- if (and (eq .Kind "page") (ne .Layout "archives") (ne .Layout "search") (not $isHLJSdisabled)) }}
@ -34,6 +36,11 @@
document.getElementById('menu').scrollLeft = localStorage.getItem("menu-scroll-position"); document.getElementById('menu').scrollLeft = localStorage.getItem("menu-scroll-position");
} }
} }
function menu_on_scroll() {
localStorage.setItem("menu-scroll-position", document.getElementById('menu').scrollLeft);
}
document.querySelectorAll('a[href^="#"]').forEach(anchor => { document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener("click", function (e) { anchor.addEventListener("click", function (e) {
e.preventDefault(); e.preventDefault();
@ -52,6 +59,11 @@
} }
}); });
}); });
</script>
{{- if (not .Site.Params.disableScrollToTop) }}
<script>
var mybutton = document.getElementById("top-link"); var mybutton = document.getElementById("top-link");
window.onscroll = function () { window.onscroll = function () {
if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) { if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) {
@ -63,11 +75,9 @@
} }
}; };
function menu_on_scroll() {
localStorage.setItem("menu-scroll-position", document.getElementById('menu').scrollLeft);
}
</script> </script>
{{- end }}
{{- if (not .Site.Params.disableThemeToggle) }} {{- if (not .Site.Params.disableThemeToggle) }}
<script> <script>
document.getElementById("theme-toggle").addEventListener("click", () => { document.getElementById("theme-toggle").addEventListener("click", () => {