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:
Aditya Telange committed 2026-10-25 11:29:27 +05:30
1 parent 26523e0fd8
commit 6be1e9170c
1 file changed
+13 -7
+13 -7
View File
@@ -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 }}