fixed 打赏卡片逻辑修复
This commit is contained in:
1 parent
1577beda42
commit
761d9024e7
4 files changed
+113
-66
No files matched your search
@@ -33,41 +33,57 @@ window.initFloatingTools = function() {
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Back to Top
|
||||
// 1. Back to Top (Optimized with IntersectionObserver)
|
||||
var backToTopButton = document.getElementById('back-to-top');
|
||||
if (backToTopButton) {
|
||||
var ticking = false;
|
||||
|
||||
function update() {
|
||||
var btn = document.getElementById('back-to-top');
|
||||
if (!btn) return;
|
||||
|
||||
// Only show if NOT on homepage (redundant if container is hidden, but good for safety)
|
||||
if (window.scrollY > 300) {
|
||||
if (!btn.classList.contains('show')) {
|
||||
btn.classList.add('show');
|
||||
}
|
||||
} else {
|
||||
if (btn.classList.contains('show')) {
|
||||
btn.classList.remove('show');
|
||||
}
|
||||
}
|
||||
ticking = false;
|
||||
|
||||
// Clean up old scroll handler if exists (from previous Pjax or version)
|
||||
if (window.backToTopScrollHandler) {
|
||||
window.removeEventListener('scroll', window.backToTopScrollHandler);
|
||||
window.backToTopScrollHandler = null;
|
||||
}
|
||||
|
||||
// Global Scroll Handler (Singleton)
|
||||
if (!window.backToTopScrollHandler) {
|
||||
// Create or reuse sentinel for IntersectionObserver
|
||||
var sentinelId = 'back-to-top-sentinel';
|
||||
var sentinel = document.getElementById(sentinelId);
|
||||
if (!sentinel) {
|
||||
sentinel = document.createElement('div');
|
||||
sentinel.id = sentinelId;
|
||||
// Sentinel covers the top 300px. When it exits viewport, we show button.
|
||||
sentinel.style.cssText = 'position: absolute; top: 0; left: 0; width: 1px; height: 300px; pointer-events: none; visibility: hidden; z-index: -9999;';
|
||||
document.body.appendChild(sentinel);
|
||||
}
|
||||
|
||||
if (window.IntersectionObserver) {
|
||||
var observer = new IntersectionObserver(function(entries) {
|
||||
entries.forEach(function(entry) {
|
||||
if (!entry.isIntersecting) {
|
||||
// Sentinel is out of view (scrolled down past 300px)
|
||||
backToTopButton.classList.add('show');
|
||||
} else {
|
||||
// Sentinel is in view (near top)
|
||||
backToTopButton.classList.remove('show');
|
||||
}
|
||||
});
|
||||
});
|
||||
observer.observe(sentinel);
|
||||
} else {
|
||||
// Fallback for very old browsers
|
||||
var ticking = false;
|
||||
var update = function() {
|
||||
if (window.scrollY > 300) {
|
||||
backToTopButton.classList.add('show');
|
||||
} else {
|
||||
backToTopButton.classList.remove('show');
|
||||
}
|
||||
ticking = false;
|
||||
};
|
||||
window.backToTopScrollHandler = function() {
|
||||
// Performance Optimization: Check scroll less frequently?
|
||||
// No, rAF is best. But we can add a check if the button is even in DOM/Visible?
|
||||
// But checking DOM layout is expensive.
|
||||
// Keep rAF but ensure 'update' is light.
|
||||
if (!ticking) {
|
||||
window.requestAnimationFrame(update);
|
||||
ticking = true;
|
||||
}
|
||||
};
|
||||
// Use passive listener to prevent scroll blocking
|
||||
window.addEventListener('scroll', window.backToTopScrollHandler, { passive: true });
|
||||
}
|
||||
|
||||
@@ -78,9 +94,6 @@ window.initFloatingTools = function() {
|
||||
backToTopButton = newBtn;
|
||||
}
|
||||
|
||||
// Initialize state immediately
|
||||
update();
|
||||
|
||||
backToTopButton.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
window.scrollTo({
|
||||
|
||||
Reference in new issue
Block a user