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" . }}
<script>
(function() {
let menu = document.getElementById('menu')
if (menu) {
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 => {
anchor.addEventListener("click", function (e) {
e.preventDefault();
var id = this.getAttribute("href").substr(1);
if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({
behavior: "smooth"
});
} else {
document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView();
}
document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({
behavior: "smooth"
});
if (id === "top") {
history.replaceState(null, null, " ");
} else {
@ -50,7 +52,7 @@
}
});
});
})();
</script>
{{- if (not site.Params.disableScrollToTop) }}