修复bug

This commit is contained in:
zqlit committed 2026-05-24 21:34:06 +08:00
1 parent 331af9074a
commit e6d50f3611
3 files changed
+107 -118

No files matched your search

+2 -26
View File
@@ -45,32 +45,6 @@ html {
scroll-behavior: smooth; scroll-behavior: smooth;
} }
/* ── Theme switch transition ── */
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 0.45s;
animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
::view-transition-old(root) {
animation-name: theme-fade-out;
}
::view-transition-new(root) {
animation-name: theme-fade-in;
animation-delay: 0.04s;
}
@keyframes theme-fade-out {
from { opacity: 1; }
to { opacity: 0; }
}
@keyframes theme-fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
[data-theme="dark"] body { [data-theme="dark"] body {
color: var(--text-content-dark-color); color: var(--text-content-dark-color);
background-color: var(--background-dark-color); background-color: var(--background-dark-color);
@@ -4533,6 +4507,8 @@ img[atk-emoticon] {
/* --- Global Theme Transition --- */ /* --- Global Theme Transition --- */
body, body,
.main, .main,
body,
.main,
.header, .header,
.footer, .footer,
.post-content, .post-content,
+24 -11
View File
@@ -123,20 +123,33 @@ window.initFloatingTools = function() {
}; };
updateIcon(); updateIcon();
themeToggle.addEventListener('click', function() { themeToggle.addEventListener('click', function(e) {
var currentTheme = document.documentElement.getAttribute('data-theme'); var currentTheme = document.documentElement.getAttribute('data-theme');
var newTheme = currentTheme === 'dark' ? 'light' : 'dark'; 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');
}
if (window.showToast) { if (typeof themeSweep === 'function') {
window.showToast('已切换至 ' + (newTheme === 'dark' ? '深色' : '浅色') + '模式', 'success'); var apply = function() {
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
updateIcon();
if (window.artalk && typeof window.artalk.setDarkMode === 'function') {
window.artalk.setDarkMode(newTheme === 'dark');
}
if (window.showToast) {
window.showToast('已切换至 ' + (newTheme === 'dark' ? '深色' : '浅色') + '模式', 'success');
}
};
themeSweep(newTheme, apply);
} else {
document.documentElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
updateIcon();
if (window.artalk && typeof window.artalk.setDarkMode === 'function') {
window.artalk.setDarkMode(newTheme === 'dark');
}
if (window.showToast) {
window.showToast('已切换至 ' + (newTheme === 'dark' ? '深色' : '浅色') + '模式', 'success');
}
} }
}); });
} }
+81 -81
View File
@@ -1,11 +1,50 @@
// 判断是否为移动端 // Scanner — page is blanked, beam paints the new theme back line by line
window.isMobile = function() { function themeSweep(newTheme, callback) {
if (window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) { var isH = Math.random() < 0.5;
return true; // 移动端 var w = window.innerWidth, h = window.innerHeight;
} else {
return false; // PC端 // Blank overlay: covers entire page with new-theme's dominant color
} var blank = newTheme === 'dark' ? '#111827' : '#fcfaf5';
}; var sheet = document.createElement('div');
sheet.style.cssText =
'position:fixed;inset:0;z-index:9999;' +
'background:' + blank + ';' +
(isH
? 'clip-path:inset(0 0 0 0);transition:clip-path 1.2s cubic-bezier(0.4,0,0.2,1);will-change:clip-path;'
: 'clip-path:inset(0 0 0 0);transition:clip-path 1.2s cubic-bezier(0.4,0,0.2,1);will-change:clip-path;');
document.body.appendChild(sheet);
// Thin scan beam at the leading edge
var beam = document.createElement('div');
beam.style.cssText =
'position:fixed;z-index:10000;pointer-events:none;' +
'background:rgba(255,255,255,0.7);' +
'box-shadow:0 0 4px 1px rgba(255,255,255,0.35),0 0 12px 2px rgba(255,255,255,0.12);' +
'transition:transform 1.2s cubic-bezier(0.4,0,0.2,1);will-change:transform;' +
(isH
? 'top:0;right:0;width:1px;height:100%;transform:translateX(0);'
: 'bottom:0;left:0;height:1px;width:100%;transform:translateY(0);');
document.body.appendChild(beam);
sheet.offsetWidth;
// Switch theme immediately — sheet covers it, beam reveals
callback();
if (isH) {
sheet.style.clipPath = 'inset(0 0 0 100%)';
beam.style.transform = 'translateX(' + (-w) + 'px)';
} else {
sheet.style.clipPath = 'inset(100% 0 0 0)';
beam.style.transform = 'translateY(' + (-h) + 'px)';
}
beam.addEventListener('transitionend', function(e) {
if (e.propertyName !== 'transform') return;
if (sheet.parentNode) sheet.parentNode.removeChild(sheet);
if (beam.parentNode) beam.parentNode.removeChild(beam);
});
}
// Toast Message Helper // Toast Message Helper
window.showToast = function(text, type) { window.showToast = function(text, type) {
@@ -21,13 +60,12 @@ window.showToast = function(text, type) {
// Theme Toggle Logic // Theme Toggle Logic
window.initThemeToggle = function() { window.initThemeToggle = function() {
const avatar = document.getElementById('theme-toggle-avatar'); var avatar = document.getElementById('theme-toggle-avatar');
if (!avatar) return; if (!avatar) return;
// Remove existing listeners to avoid duplicates (if any) var newAvatar = avatar.cloneNode(true);
const newAvatar = avatar.cloneNode(true);
avatar.parentNode.replaceChild(newAvatar, avatar); avatar.parentNode.replaceChild(newAvatar, avatar);
newAvatar.addEventListener('click', function(e) { newAvatar.addEventListener('click', function(e) {
e.preventDefault(); e.preventDefault();
@@ -38,49 +76,34 @@ window.initThemeToggle = function() {
var apply = function() { var apply = function() {
html.setAttribute('data-theme', newTheme); html.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme); localStorage.setItem('theme', newTheme);
window.showToast('已切换至 ' + (newTheme === 'dark' ? '深色' : '浅色') + '模式', 'success'); window.showToast('已切换至 ' + (newTheme === 'dark' ? '深色' : '浅色') + '模式', 'success');
if (window.artalk && typeof window.artalk.setDarkMode === 'function') { if (window.artalk && typeof window.artalk.setDarkMode === 'function') {
window.artalk.setDarkMode(newTheme === 'dark'); window.artalk.setDarkMode(newTheme === 'dark');
} }
window.dispatchEvent(new Event('resize')); window.dispatchEvent(new Event('resize'));
}; };
var isMobile = window.isMobile && window.isMobile(); themeSweep(newTheme, apply);
if (document.startViewTransition && !isMobile) {
document.startViewTransition(function() { apply(); });
} else {
apply();
}
}); });
}; };
// Code Folding Logic // Code Folding Logic
window.initCodeFold = function() { window.initCodeFold = function() {
const codeBlocks = document.querySelectorAll('.highlight'); var codeBlocks = document.querySelectorAll('.highlight');
codeBlocks.forEach(block => { codeBlocks.forEach(function(block) {
// Check height (approx 300px)
if (block.offsetHeight > 360) { if (block.offsetHeight > 360) {
// Check if already processed
if (block.parentNode.classList.contains('code-fold-container')) return; if (block.parentNode.classList.contains('code-fold-container')) return;
// Wrap var wrapper = document.createElement('div');
const wrapper = document.createElement('div');
wrapper.className = 'code-fold-container collapsed'; wrapper.className = 'code-fold-container collapsed';
block.parentNode.insertBefore(wrapper, block); block.parentNode.insertBefore(wrapper, block);
wrapper.appendChild(block); wrapper.appendChild(block);
// Add Button var btn = document.createElement('button');
const btn = document.createElement('button');
btn.className = 'code-expand-btn'; btn.className = 'code-expand-btn';
btn.innerHTML = '<span><i class="ri-arrow-down-s-line" style="margin-right:4px;"></i> 展开代码 </span>'; btn.innerHTML = '<span><i class="ri-arrow-down-s-line" style="margin-right:4px;"></i> 展开代码 </span>';
btn.onclick = function() { btn.onclick = function() {
wrapper.classList.remove('collapsed'); wrapper.classList.remove('collapsed');
// Hide button after expand, or change to collapse?
// Usually just expand is enough.
btn.style.display = 'none'; btn.style.display = 'none';
}; };
wrapper.appendChild(btn); wrapper.appendChild(btn);
@@ -90,62 +113,40 @@ window.initCodeFold = function() {
// Code Copy Logic // Code Copy Logic
window.initCodeCopy = function() { window.initCodeCopy = function() {
// Target pre tags that contain code, or .highlight containers var codeBlocks = document.querySelectorAll('pre');
const codeBlocks = document.querySelectorAll('pre');
codeBlocks.forEach(function(pre) {
codeBlocks.forEach(pre => {
// Avoid double init
if (pre.parentNode.querySelector('.copy-code-btn')) return; if (pre.parentNode.querySelector('.copy-code-btn')) return;
if (pre.querySelector('.copy-code-btn')) return; if (pre.querySelector('.copy-code-btn')) return;
// Check if it's inside a highlight block (Hugo usually does .highlight > pre) var btn = document.createElement('button');
// We attach button to the pre or the highlight container?
// Attaching to pre is safer for positioning.
const btn = document.createElement('button');
btn.className = 'copy-code-btn'; btn.className = 'copy-code-btn';
btn.innerHTML = '<i class="ri-file-copy-line"></i>'; btn.innerHTML = '<i class="ri-file-copy-line"></i>';
btn.title = "复制代码"; btn.title = "复制代码";
btn.style.cssText = ` btn.style.cssText =
position: absolute; 'position:absolute;top:-6px;right:5px;background:transparent;border:none;' +
top: -6px; 'color:#888;cursor:pointer;padding:5px;border-radius:4px;' +
right: 5px; 'transition:all 0.2s;opacity:0;z-index:10;';
background: transparent;
border: none;
color: #888;
cursor: pointer;
padding: 5px;
border-radius: 4px;
transition: all 0.2s;
opacity: 0;
z-index: 10;
`;
// Show on hover
pre.addEventListener('mouseenter', () => btn.style.opacity = '1');
pre.addEventListener('mouseleave', () => btn.style.opacity = '0');
// Handle touch devices
if (window.isMobile()) {
btn.style.opacity = '1';
}
btn.addEventListener('click', (e) => { pre.addEventListener('mouseenter', function() { btn.style.opacity = '1'; });
pre.addEventListener('mouseleave', function() { btn.style.opacity = '0'; });
if (window.isMobile()) { btn.style.opacity = '1'; }
btn.addEventListener('click', function(e) {
e.stopPropagation(); e.stopPropagation();
const code = pre.querySelector('code') ? pre.querySelector('code').innerText : pre.innerText; var code = pre.querySelector('code') ? pre.querySelector('code').innerText : pre.innerText;
if (navigator.clipboard) { if (navigator.clipboard) {
navigator.clipboard.writeText(code).then(() => { navigator.clipboard.writeText(code).then(function() {
window.showToast('代码已复制 / Copied!', 'success'); window.showToast('代码已复制 / Copied!', 'success');
btn.innerHTML = '<i class="ri-check-line"></i>'; btn.innerHTML = '<i class="ri-check-line"></i>';
setTimeout(() => btn.innerHTML = '<i class="ri-file-copy-line"></i>', 2000); setTimeout(function() { btn.innerHTML = '<i class="ri-file-copy-line"></i>'; }, 2000);
}).catch(err => { }).catch(function() {
console.error('Copy failed', err);
window.showToast('复制失败 / Failed', 'error'); window.showToast('复制失败 / Failed', 'error');
}); });
} else { } else {
// Fallback var textarea = document.createElement('textarea');
const textarea = document.createElement('textarea');
textarea.value = code; textarea.value = code;
document.body.appendChild(textarea); document.body.appendChild(textarea);
textarea.select(); textarea.select();
@@ -154,12 +155,11 @@ window.initCodeCopy = function() {
window.showToast('代码已复制 / Copied!', 'success'); window.showToast('代码已复制 / Copied!', 'success');
} }
}); });
// Ensure relative positioning
if (getComputedStyle(pre).position === 'static') { if (getComputedStyle(pre).position === 'static') {
pre.style.position = 'relative'; pre.style.position = 'relative';
} }
pre.appendChild(btn); pre.appendChild(btn);
}); });
}; };