add param DisableSmoothScroll

This commit is contained in:
Reorx 2022-04-10 17:43:04 +08:00
parent 829bb01691
commit d50ce4575d
1 changed files with 10 additions and 8 deletions

View File

@ -24,6 +24,7 @@
{{- partial "extend_footer.html" . }} {{- partial "extend_footer.html" . }}
<script> <script>
(function() {
let menu = document.getElementById('menu') let menu = document.getElementById('menu')
if (menu) { if (menu) {
menu.scrollLeft = localStorage.getItem("menu-scroll-position"); menu.scrollLeft = localStorage.getItem("menu-scroll-position");
@ -32,17 +33,18 @@
} }
} }
const disableSmoothScroll = '{{- if (.Param "DisableSmoothScroll") -}}1{{- end -}}' == '1';
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches || disableSmoothScroll) {
return;
}
// only run this code if smooth scroll should be enabled
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();
var id = this.getAttribute("href").substr(1); var id = this.getAttribute("href").substr(1);
if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) { document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({
document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({ behavior: "smooth"
behavior: "smooth" });
});
} else {
document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView();
}
if (id === "top") { if (id === "top") {
history.replaceState(null, null, " "); history.replaceState(null, null, " ");
} else { } else {
@ -50,7 +52,7 @@
} }
}); });
}); });
})();
</script> </script>
{{- if (not site.Params.disableScrollToTop) }} {{- if (not site.Params.disableScrollToTop) }}