refactor(背景): 重构背景图片实现并更新相关配置

- 将背景图片从CSS伪元素改为独立的div元素
- 移除背景图片加载检测逻辑
- 更新文章和配置中的图片URL
- 禁用默认背景图片功能
This commit is contained in:
二叉树树
2025-12-28 14:28:33 +08:00
parent 99f88bf36a
commit d43f929d20
4 changed files with 42 additions and 44 deletions

View File

@@ -115,7 +115,7 @@ const bannerOffset =
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<script src="/js/umami-share.js" defer></script>
{/* <!-- <script data-swup-ignore-script is:inline src="https://pic.acofork.com/random.js"></script> --> */}
<script data-swup-ignore-script is:inline src="https://pic.acofork.com/random.js"></script>
@@ -163,27 +163,27 @@ const bannerOffset =
offset = offset - offset % 4;
document.documentElement.style.setProperty('--banner-height-extend', `${offset}px`);
// Background image loading detection
const bgUrl = getComputedStyle(document.documentElement).getPropertyValue('--bg-url').trim();
const bgEnable = getComputedStyle(document.documentElement).getPropertyValue('--bg-enable').trim();
// // Background image loading detection
// const bgUrl = getComputedStyle(document.documentElement).getPropertyValue('--bg-url').trim();
// const bgEnable = getComputedStyle(document.documentElement).getPropertyValue('--bg-enable').trim();
if (bgUrl && bgUrl !== 'none' && bgEnable === '1') {
const img = new Image();
const urlMatch = bgUrl.match(/url\(["']?([^"')]+)["']?\)/);
if (urlMatch) {
img.onload = function() {
// 背景图片完全加载后,显示背景并启用卡片透明效果
document.body.classList.add('bg-loaded');
document.documentElement.style.setProperty('--card-bg', 'var(--card-bg-transparent)');
document.documentElement.style.setProperty('--float-panel-bg', 'var(--float-panel-bg-transparent)');
};
img.onerror = function() {
// Keep cards opaque if background image fails to load
console.warn('Background image failed to load, keeping cards opaque');
};
img.src = urlMatch[1];
}
}
// if (bgUrl && bgUrl !== 'none' && bgEnable === '1') {
// const img = new Image();
// const urlMatch = bgUrl.match(/url\(["']?([^"')]+)["']?\)/);
// if (urlMatch) {
// img.onload = function() {
// // 背景图片完全加载后,显示背景并启用卡片透明效果
// document.body.classList.add('bg-loaded');
// document.documentElement.style.setProperty('--card-bg', 'var(--card-bg-transparent)');
// document.documentElement.style.setProperty('--float-panel-bg', 'var(--float-panel-bg-transparent)');
// };
// img.onerror = function() {
// // Keep cards opaque if background image fails to load
// console.warn('Background image failed to load, keeping cards opaque');
// };
// img.src = urlMatch[1];
// }
// }
</script>
<style define:vars={{
configHue,
@@ -217,27 +217,24 @@ const bannerOffset =
--bg-opacity: var(--bg-opacity);
}
body::before {
content: '' !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
width: 100% !important;
height: 100% !important;
background-image: var(--bg-url) !important;
background-position: var(--bg-position) !important;
background-size: var(--bg-size) !important;
background-repeat: var(--bg-repeat) !important;
background-attachment: var(--bg-attachment) !important;
opacity: 0 !important;
pointer-events: none !important;
z-index: -1 !important;
display: block !important;
transition: opacity 0.3s ease-in-out !important;
#bg-box {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-position: var(--bg-position);
background-size: var(--bg-size);
background-repeat: var(--bg-repeat);
background-attachment: var(--bg-attachment);
opacity: 0;
pointer-events: none;
z-index: -1;
transition: opacity 0.3s ease-in-out;
}
body.bg-loaded::before {
opacity: calc(var(--bg-opacity) * var(--bg-enable)) !important;
#bg-box.loaded {
opacity: calc(var(--bg-opacity) * var(--bg-enable));
}
</style> <!-- defines global css variables. This will be applied to <html> <body> and some other elements idk why -->
@@ -289,6 +286,7 @@ var _hmt = _hmt || [];
<body class=" min-h-screen transition " class:list={[{"lg:is-home": isHomePage, "enable-banner": enableBanner}]}
data-overlayscrollbars-initialize
>
<div id="bg-box"></div>
<ConfigCarrier></ConfigCarrier>
<slot />