mirror of
https://github.com/adityatelange/hugo-PaperMod.git
synced 2026-11-22 12:39:04 +00:00
perf(footer): throttle scroll-to-top check to once per frame
Uses a passive scroll listener with requestAnimationFrame instead of assigning window.onscroll, which also no longer clobbers or gets clobbered by other window.onscroll handlers.
This commit is contained in:
1 file changed
+13
-7
@@ -79,14 +79,20 @@
|
||||
{{- if (not site.Params.disableScrollToTop) }}
|
||||
<script>
|
||||
var toplink = document.getElementById("top-link");
|
||||
window.onscroll = function () {
|
||||
const scrollThreshold = window.innerHeight;
|
||||
if (document.body.scrollTop > scrollThreshold || document.documentElement.scrollTop > scrollThreshold) {
|
||||
toplink.classList.remove("hidden");
|
||||
} else {
|
||||
toplink.classList.add("hidden");
|
||||
var scrollTicking = false;
|
||||
// At most one check per frame
|
||||
window.addEventListener("scroll", function () {
|
||||
if (scrollTicking) {
|
||||
return;
|
||||
}
|
||||
};
|
||||
scrollTicking = true;
|
||||
requestAnimationFrame(function () {
|
||||
const scrollThreshold = window.innerHeight;
|
||||
const shown = document.body.scrollTop > scrollThreshold || document.documentElement.scrollTop > scrollThreshold;
|
||||
toplink.classList.toggle("hidden", !shown);
|
||||
scrollTicking = false;
|
||||
});
|
||||
}, { passive: true });
|
||||
|
||||
</script>
|
||||
{{- end }}
|
||||
|
||||
Reference in new issue
Block a user