fixed 昼夜切换 bug
This commit is contained in:
1 parent
aaf8813d8b
commit
7de8d0e253
6 files changed
+211
-122
No files matched your search
@@ -61,6 +61,7 @@
|
||||
this.index = 0;
|
||||
this.timer = null;
|
||||
this.isPaused = false;
|
||||
this.isDestroyed = false;
|
||||
}
|
||||
|
||||
// 开始发送
|
||||
@@ -70,11 +71,13 @@
|
||||
if (this.data.length === 0) return;
|
||||
|
||||
this.clear();
|
||||
this.isDestroyed = false;
|
||||
this.loop();
|
||||
}
|
||||
|
||||
// 清空
|
||||
clear() {
|
||||
this.isDestroyed = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
// 清除容器内所有弹幕元素
|
||||
if (this.container) {
|
||||
@@ -86,18 +89,12 @@
|
||||
|
||||
// 循环发射
|
||||
loop() {
|
||||
// 如果容器不在文档中(PJAX切换后),尝试重新获取容器
|
||||
// (注意:this.container 是 DOM 引用,如果页面重绘,旧引用可能失效)
|
||||
if (this.isDestroyed) return;
|
||||
|
||||
// 如果容器不在文档中(PJAX切换后),停止播放
|
||||
if (!document.body.contains(this.container)) {
|
||||
console.warn('[Barrage] 容器失效,尝试重新获取...');
|
||||
const newContainer = document.querySelector('.barrager');
|
||||
if (newContainer) {
|
||||
this.container = newContainer;
|
||||
console.log('[Barrage] 容器重新获取成功,继续播放');
|
||||
} else {
|
||||
console.warn('[Barrage] 容器彻底丢失,停止播放');
|
||||
return; // 真的没了再停止
|
||||
}
|
||||
console.warn('[Barrage] 容器失效,停止播放');
|
||||
return;
|
||||
}
|
||||
|
||||
// 发射逻辑
|
||||
@@ -285,15 +282,18 @@
|
||||
console.error('加载弹幕时发生错误:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 暴露给全局,供 mypjax.js 调用
|
||||
window.loadBarrager = loadBarrager;
|
||||
|
||||
// 初始化监听
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
loadBarrager(); // 页面首次加载时启动弹幕
|
||||
});
|
||||
|
||||
document.addEventListener('pjax:complete', function() {
|
||||
// PJAX 跳转后重新加载弹幕
|
||||
loadBarrager();
|
||||
});
|
||||
// 移除内部的 pjax:complete 监听,统一由 mypjax.js 控制调用时机
|
||||
// document.addEventListener('pjax:complete', function() {
|
||||
// loadBarrager();
|
||||
// });
|
||||
|
||||
})();
|
||||
@@ -218,17 +218,47 @@ function copySzhContent() {
|
||||
}
|
||||
}
|
||||
|
||||
// Update Theme Icon
|
||||
function updateThemeIcon(isDark) {
|
||||
const toggleBtns = document.querySelectorAll('#theme-toggle');
|
||||
toggleBtns.forEach(btn => {
|
||||
const icon = btn.querySelector('i');
|
||||
if (icon) {
|
||||
// Add rotation class for animation
|
||||
btn.classList.add('rotate');
|
||||
setTimeout(() => btn.classList.remove('rotate'), 500);
|
||||
|
||||
// Switch icon
|
||||
if (isDark) {
|
||||
icon.className = 'ri-moon-line';
|
||||
} else {
|
||||
icon.className = 'ri-sun-line';
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Theme Toggle Logic
|
||||
function toggleTheme() {
|
||||
const currentTheme = document.documentElement.getAttribute('data-theme');
|
||||
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
|
||||
const isDark = newTheme === 'dark';
|
||||
|
||||
document.documentElement.setAttribute('data-theme', newTheme);
|
||||
localStorage.setItem('theme', newTheme);
|
||||
|
||||
// Notify Artalk if present
|
||||
if (window.Artalk) {
|
||||
Artalk.setDarkMode(newTheme === 'dark');
|
||||
}
|
||||
// Update Icon
|
||||
updateThemeIcon(isDark);
|
||||
|
||||
// Optimize: Defer heavy Artalk update to avoid blocking UI animation
|
||||
setTimeout(() => {
|
||||
// Notify Artalk if present (Instance or Static)
|
||||
if (window.artalk && typeof window.artalk.setDarkMode === 'function') {
|
||||
window.artalk.setDarkMode(isDark);
|
||||
} else if (window.Artalk && typeof Artalk.setDarkMode === 'function') {
|
||||
Artalk.setDarkMode(isDark);
|
||||
}
|
||||
}, 50);
|
||||
}
|
||||
|
||||
// Listen for system theme changes
|
||||
@@ -239,7 +269,13 @@ try {
|
||||
if (!localStorage.getItem('theme')) {
|
||||
const newTheme = e.matches ? 'dark' : 'light';
|
||||
document.documentElement.setAttribute('data-theme', newTheme);
|
||||
if (window.Artalk) {
|
||||
|
||||
updateThemeIcon(newTheme === 'dark');
|
||||
|
||||
// Notify Artalk
|
||||
if (window.artalk && typeof window.artalk.setDarkMode === 'function') {
|
||||
window.artalk.setDarkMode(newTheme === 'dark');
|
||||
} else if (window.Artalk && typeof Artalk.setDarkMode === 'function') {
|
||||
Artalk.setDarkMode(newTheme === 'dark');
|
||||
}
|
||||
}
|
||||
@@ -279,6 +315,21 @@ function updateFloatingToolsVisibility() {
|
||||
|
||||
// Floating Tools Initialization
|
||||
function initFloatingTools() {
|
||||
// Initialize Theme Icon based on current theme
|
||||
const currentTheme = document.documentElement.getAttribute('data-theme');
|
||||
const isDark = currentTheme === 'dark';
|
||||
const toggleBtns = document.querySelectorAll('#theme-toggle');
|
||||
toggleBtns.forEach(btn => {
|
||||
const icon = btn.querySelector('i');
|
||||
if (icon) {
|
||||
if (isDark) {
|
||||
icon.className = 'ri-moon-line';
|
||||
} else {
|
||||
icon.className = 'ri-sun-line';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Initial visibility check
|
||||
updateFloatingToolsVisibility();
|
||||
|
||||
|
||||
@@ -190,7 +190,8 @@ document.addEventListener("pjax:complete", function () {
|
||||
emoticons = window.siteConfig.artalk.emoticons || emoticons;
|
||||
}
|
||||
|
||||
Artalk.init({
|
||||
// 保存实例到全局变量 window.artalk
|
||||
window.artalk = Artalk.init({
|
||||
el: '#Comments', // 绑定元素的 Selector
|
||||
server: serverUrl, // 后端地址
|
||||
site: siteName, // 你的站点名
|
||||
@@ -201,10 +202,11 @@ document.addEventListener("pjax:complete", function () {
|
||||
darkMode: isDark,
|
||||
emoticons: emoticons
|
||||
})
|
||||
}
|
||||
// 重载评论弹幕
|
||||
if (document.querySelector("#Comments") && !isMobile()) {
|
||||
loadBarrager()
|
||||
|
||||
// Artalk 初始化完成后,再加载弹幕
|
||||
if (!isMobile() && window.loadBarrager) {
|
||||
window.loadBarrager();
|
||||
}
|
||||
}
|
||||
// 重载不蒜子统计
|
||||
if (typeof window.bsz_fetch === 'function') {
|
||||
|
||||
Reference in new issue
Block a user