Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
31 lines
975 B
JavaScript
31 lines
975 B
JavaScript
(function () {
|
|
var scrollToTopButton = document.getElementById("scrollToTopButton");
|
|
if (!scrollToTopButton) return;
|
|
|
|
var ticking = false;
|
|
var lastVisible = null;
|
|
|
|
function updateVisibility() {
|
|
ticking = false;
|
|
// 用 visualViewport / 近似阈值,避免反复读 layout 几何属性触发强制重排
|
|
var threshold = Math.max(window.innerHeight || 600, 600);
|
|
var shouldShow = (window.scrollY || window.pageYOffset || 0) > threshold;
|
|
if (shouldShow === lastVisible) return;
|
|
lastVisible = shouldShow;
|
|
scrollToTopButton.classList.toggle("is-visible", shouldShow);
|
|
}
|
|
|
|
function onScroll() {
|
|
if (ticking) return;
|
|
ticking = true;
|
|
window.requestAnimationFrame(updateVisibility);
|
|
}
|
|
|
|
scrollToTopButton.addEventListener("click", function () {
|
|
window.scrollTo({ top: 0, behavior: "smooth" });
|
|
});
|
|
|
|
window.addEventListener("scroll", onScroll, { passive: true });
|
|
window.requestAnimationFrame(updateVisibility);
|
|
})();
|