fixed 工具栏bug 修复
This commit is contained in:
1 parent
8c3232fadf
commit
534ed6842b
2 files changed
+182
-101
No files matched your search
@@ -1,106 +1,171 @@
|
|||||||
window.initFloatingTools = function() {
|
window.initFloatingTools = function() {
|
||||||
// 1. Back to Top
|
try {
|
||||||
var backToTopButton = document.getElementById('back-to-top');
|
// 0. Global Toolbar Visibility (Homepage Check)
|
||||||
if (backToTopButton) {
|
var container = document.querySelector('.floating-tools');
|
||||||
var ticking = false;
|
|
||||||
|
// Robust Homepage Check:
|
||||||
function update() {
|
// 1. Check for Homepage-specific element (.recent-posts)
|
||||||
var btn = document.getElementById('back-to-top');
|
// 2. Ensure NOT on a Post page (.post-content)
|
||||||
if (!btn) return;
|
// 3. Fallback: Check pathname (only if DOM check is ambiguous)
|
||||||
|
var hasRecentPosts = document.querySelector('.recent-posts');
|
||||||
if (window.scrollY > 300) {
|
var hasPostContent = document.querySelector('.post-content');
|
||||||
if (!btn.classList.contains('show')) {
|
|
||||||
btn.classList.add('show');
|
var isHomePage = false;
|
||||||
}
|
if (hasRecentPosts && !hasPostContent) {
|
||||||
} else {
|
isHomePage = true;
|
||||||
if (btn.classList.contains('show')) {
|
} else if (!hasRecentPosts && !hasPostContent) {
|
||||||
btn.classList.remove('show');
|
// Could be Archive or Custom Page. Check Path.
|
||||||
}
|
var path = window.location.pathname;
|
||||||
|
if (path === '/' || path === '/index.html' || path.startsWith('/page/')) {
|
||||||
|
isHomePage = true;
|
||||||
}
|
}
|
||||||
ticking = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Global Scroll Handler
|
if (container) {
|
||||||
if (!window.backToTopScrollHandler) {
|
if (isHomePage) {
|
||||||
window.backToTopScrollHandler = function() {
|
container.style.display = 'none';
|
||||||
if (!ticking) {
|
// Do not return here, let other logic run in case we need to reset state
|
||||||
window.requestAnimationFrame(update);
|
// But for performance, we can skip binding if hidden?
|
||||||
ticking = true;
|
// No, better to bind so if it becomes visible later (unlikely without PJAX) it works.
|
||||||
|
// Actually, with PJAX, this function runs again. So we can just hide it.
|
||||||
|
} else {
|
||||||
|
container.style.display = ''; // Restore display
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Back to Top
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Global Scroll Handler (Singleton)
|
||||||
|
if (!window.backToTopScrollHandler) {
|
||||||
|
window.backToTopScrollHandler = function() {
|
||||||
|
if (!ticking) {
|
||||||
|
window.requestAnimationFrame(update);
|
||||||
|
ticking = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('scroll', window.backToTopScrollHandler, { passive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-bind click event
|
||||||
|
var newBtn = backToTopButton.cloneNode(true);
|
||||||
|
if (backToTopButton.parentNode) {
|
||||||
|
backToTopButton.parentNode.replaceChild(newBtn, backToTopButton);
|
||||||
|
backToTopButton = newBtn;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize state immediately
|
||||||
|
update();
|
||||||
|
|
||||||
|
backToTopButton.addEventListener('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
window.scrollTo({
|
||||||
|
top: 0,
|
||||||
|
behavior: 'smooth'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Theme Toggle
|
||||||
|
var themeToggle = document.getElementById('theme-toggle');
|
||||||
|
if (themeToggle) {
|
||||||
|
var newToggle = themeToggle.cloneNode(true);
|
||||||
|
if (themeToggle.parentNode) {
|
||||||
|
themeToggle.parentNode.replaceChild(newToggle, themeToggle);
|
||||||
|
themeToggle = newToggle;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update icon based on current theme
|
||||||
|
var updateIcon = function() {
|
||||||
|
var currentTheme = document.documentElement.getAttribute('data-theme');
|
||||||
|
var icon = themeToggle.querySelector('i');
|
||||||
|
if (icon) {
|
||||||
|
if (currentTheme === 'dark') {
|
||||||
|
icon.className = 'ri-moon-line';
|
||||||
|
} else {
|
||||||
|
icon.className = 'ri-sun-line';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
updateIcon();
|
||||||
|
|
||||||
|
themeToggle.addEventListener('click', function() {
|
||||||
|
var currentTheme = document.documentElement.getAttribute('data-theme');
|
||||||
|
var newTheme = currentTheme === 'dark' ? 'light' : 'dark';
|
||||||
|
document.documentElement.setAttribute('data-theme', newTheme);
|
||||||
|
localStorage.setItem('theme', newTheme);
|
||||||
|
updateIcon();
|
||||||
|
|
||||||
|
// Trigger Artalk theme update
|
||||||
|
if (window.artalk && typeof window.artalk.setDarkMode === 'function') {
|
||||||
|
window.artalk.setDarkMode(newTheme === 'dark');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. To Comments
|
||||||
|
var toComments = document.getElementById('to-comments');
|
||||||
|
if (toComments) {
|
||||||
|
// Logic: Only show if comments section exists
|
||||||
|
var checkCommentVisibility = function(btn) {
|
||||||
|
if (!btn) return;
|
||||||
|
var hasComments = document.getElementById('Comments') || document.querySelector('.artalk');
|
||||||
|
|
||||||
|
if (hasComments) {
|
||||||
|
btn.style.display = ''; // Show
|
||||||
|
} else {
|
||||||
|
btn.style.display = 'none'; // Hide
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
window.addEventListener('scroll', window.backToTopScrollHandler, { passive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Initial check
|
||||||
|
checkCommentVisibility(toComments);
|
||||||
|
|
||||||
// Re-bind click event
|
var newToComments = toComments.cloneNode(true);
|
||||||
var newBtn = backToTopButton.cloneNode(true);
|
if (toComments.parentNode) {
|
||||||
backToTopButton.parentNode.replaceChild(newBtn, backToTopButton);
|
toComments.parentNode.replaceChild(newToComments, toComments);
|
||||||
backToTopButton = newBtn;
|
toComments = newToComments;
|
||||||
|
|
||||||
backToTopButton.addEventListener('click', function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
window.scrollTo({
|
|
||||||
top: 0,
|
|
||||||
behavior: 'smooth'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Theme Toggle
|
|
||||||
var themeToggle = document.getElementById('theme-toggle');
|
|
||||||
if (themeToggle) {
|
|
||||||
var newToggle = themeToggle.cloneNode(true);
|
|
||||||
themeToggle.parentNode.replaceChild(newToggle, themeToggle);
|
|
||||||
themeToggle = newToggle;
|
|
||||||
|
|
||||||
// Update icon based on current theme
|
|
||||||
var updateIcon = function() {
|
|
||||||
var currentTheme = document.documentElement.getAttribute('data-theme');
|
|
||||||
var icon = themeToggle.querySelector('i');
|
|
||||||
if (icon) {
|
|
||||||
if (currentTheme === 'dark') {
|
|
||||||
icon.className = 'ri-moon-line';
|
|
||||||
} else {
|
|
||||||
icon.className = 'ri-sun-line';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
updateIcon();
|
|
||||||
|
|
||||||
themeToggle.addEventListener('click', function() {
|
|
||||||
var currentTheme = document.documentElement.getAttribute('data-theme');
|
|
||||||
var newTheme = currentTheme === 'dark' ? 'light' : 'dark';
|
|
||||||
document.documentElement.setAttribute('data-theme', newTheme);
|
|
||||||
localStorage.setItem('theme', newTheme);
|
|
||||||
updateIcon();
|
|
||||||
|
|
||||||
// Trigger Artalk theme update
|
|
||||||
if (window.artalk) {
|
|
||||||
window.artalk.setDarkMode(newTheme === 'dark');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. To Comments
|
|
||||||
var toComments = document.getElementById('to-comments');
|
|
||||||
if (toComments) {
|
|
||||||
var newToComments = toComments.cloneNode(true);
|
|
||||||
toComments.parentNode.replaceChild(newToComments, toComments);
|
|
||||||
toComments = newToComments;
|
|
||||||
|
|
||||||
toComments.addEventListener('click', function(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
var comments = document.getElementById('Comments');
|
|
||||||
if (comments) {
|
|
||||||
comments.scrollIntoView({ behavior: 'smooth' });
|
|
||||||
} else {
|
|
||||||
var artalkEl = document.querySelector('.artalk');
|
|
||||||
if (artalkEl) {
|
|
||||||
artalkEl.scrollIntoView({ behavior: 'smooth' });
|
|
||||||
} else {
|
|
||||||
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
// Re-run check on new element
|
||||||
|
checkCommentVisibility(toComments);
|
||||||
|
|
||||||
|
// Double-check after a delay for PJAX/Async rendering
|
||||||
|
setTimeout(function() {
|
||||||
|
var currentBtn = document.getElementById('to-comments');
|
||||||
|
if (currentBtn) {
|
||||||
|
checkCommentVisibility(currentBtn);
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
|
||||||
|
toComments.addEventListener('click', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
var comments = document.getElementById('Comments') || document.querySelector('.artalk');
|
||||||
|
if (comments) {
|
||||||
|
comments.scrollIntoView({ behavior: 'smooth' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error in initFloatingTools:', e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -96,25 +96,41 @@ document.addEventListener("pjax:complete", function () {
|
|||||||
|
|
||||||
// 初始化段落评论
|
// 初始化段落评论
|
||||||
if (typeof window.initParagraphComments === 'function') {
|
if (typeof window.initParagraphComments === 'function') {
|
||||||
window.initParagraphComments();
|
try {
|
||||||
|
window.initParagraphComments();
|
||||||
|
} catch(e) { console.error('initParagraphComments error:', e); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化友链状态
|
// 初始化友链状态
|
||||||
if (typeof window.initLinkStatus === 'function') {
|
if (typeof window.initLinkStatus === 'function') {
|
||||||
window.initLinkStatus();
|
try {
|
||||||
|
window.initLinkStatus();
|
||||||
|
} catch(e) { console.error('initLinkStatus error:', e); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重载评论
|
// 重载评论
|
||||||
if (typeof window.initArtalk === 'function') {
|
if (typeof window.initArtalk === 'function') {
|
||||||
window.initArtalk();
|
try {
|
||||||
|
window.initArtalk();
|
||||||
|
} catch(e) { console.error('initArtalk error:', e); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重载不蒜子统计
|
// 重载不蒜子统计
|
||||||
if (typeof window.bsz_fetch === 'function') {
|
if (typeof window.bsz_fetch === 'function') {
|
||||||
window.bsz_fetch();
|
try {
|
||||||
|
window.bsz_fetch();
|
||||||
|
} catch(e) { console.error('bsz_fetch error:', e); }
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重载 Search 和 Floating Tools (Moved from main.js)
|
// 重载 Search 和 Floating Tools (Moved from main.js)
|
||||||
if (typeof window.initFloatingTools === 'function') window.initFloatingTools();
|
if (typeof window.initFloatingTools === 'function') {
|
||||||
if (typeof window.initSearch === 'function') window.initSearch();
|
try {
|
||||||
|
window.initFloatingTools();
|
||||||
|
} catch(e) { console.error('initFloatingTools error:', e); }
|
||||||
|
}
|
||||||
|
if (typeof window.initSearch === 'function') {
|
||||||
|
try {
|
||||||
|
window.initSearch();
|
||||||
|
} catch(e) { console.error('initSearch error:', e); }
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user