code.html
code.html
/* 主要的覆蓋開始 */
body.dark-mode .components {
background-color: rgba(25, 30, 50, 1) !important; /* 暗黑模式背景 */
}
/* 排除按鈕相關元素不受 dark-mode 影響 */
body.dark-mode .components .main-button,
body.dark-mode .components .moon,
body.dark-mode .components .daytime-background,
body.dark-mode .components .cloud,
body.dark-mode .components .cloud-light,
body.dark-mode .components .stars,
body.dark-mode .components .star,
body.dark-mode .components .cloud-son,
body.dark-mode .components .star-son {
background-color: inherit !important;
color: inherit !important;
}
/* 保持原有的按鈕樣式 */
body.dark-mode .components .main-button {
background-color: rgba(195, 200, 210, 1) !important;
box-shadow: 1.8px 1.8px 3px rgba(0, 0, 0, 0.5),
inset -1.8px -2.8px 1.8px -1.8px rgba(0, 0, 0, 0.5),
inset 2.3px 2.8px 1.2px -1.2px rgba(255, 255, 210, 1) !
important;
}
/* 保持月亮的顏色 */
body.dark-mode .components .moon {
background-color: rgba(150, 160, 180, 1) !important;
}
/* 保持雲朵的顏色 */
body.dark-mode .components .cloud-son,
body.dark-mode .components .cloud-light .cloud-son {
background-color: #fff !important;
}
/* 保持星星的顏色 */
body.dark-mode .components .star-son {
background-image: radial-gradient(circle var(--size) at var(--pos),
transparent var(--size), #fff) !important;
}
/* 保持日間背景的透明效果 */
body.dark-mode .components .daytime-background:nth-child(2) {
background-color: rgba(255, 255, 255, 0.2) !important;
}
/* 新增部分 */
body.dark-mode #wpadminbar,
body.dark-mode #wpadminbar * {
background-color: #222 !important;
color: #eee !important;
}
body.dark-mode .wp-block-navigation *,
body.dark-mode .wp-block-navigation {
background-color: #222 !important;
}
body.dark-mode .wp-block-buttons .wp-block-button a,
body.dark-mode .wp-block-buttons .wp-block-button__link {
background-color: #444 !important;
color: #eee !important;
border: 1px solid #555 !important;
}
/* 加強覆蓋 */
body.dark-mode div,
body.dark-mode section,
body.dark-mode article,
body.dark-mode header,
body.dark-mode footer,
body.dark-mode main,
body.dark-mode aside {
background-color: #222 !important;
}
body.dark-mode #wpcontent {
background-color: #222 !important;
}
body.dark-mode .wp-block-separator{
border-color: #ddd !important;
background-color: #ddd !important;
}
/* 主要的覆蓋結束 */
</style>
<script>
(() => {
const func = (root, initTheme, changeTheme) => {
const $ = (s) => {
let dom = root.querySelectorAll(s);
return dom.length == 1 ? dom[0] : dom;
};
let mainButton = $(".main-button");
let daytimeBackground = $(".daytime-background");
let cloud = $(".cloud");
let cloudList = $(".cloud-son");
let cloudLight = $(".cloud-light");
let components = $(".components");
let moon = $(".moon");
let stars = $(".stars");
let star = $(".star");
let isMoved = false; // 追蹤是否切換主題
let isClicked = false; // 避免快速連續點擊
let body = document.body;
window.matchMedia("(prefers-color-scheme:
dark)").addEventListener("change", () => {
toggleThemeBasedOnSystem();
});
const toggleThemeBasedOnSystem = () => {
if (window.matchMedia("(prefers-color-scheme: dark)").matches)
{
if (!isMoved) {
components.onclick();
}
} else {
if (isMoved) {
components.onclick();
}
}
};
components.onclick = () => { // 點擊容器切換主題
if (isMoved) { // 當前為暗黑模式,切換為白天模式
body.classList.remove('dark-mode');
mainButton.style.transform = "translateX(0)";
mainButton.style.backgroundColor = "rgba(255, 195, 35,1)";
mainButton.style.boxShadow =
"1.8px 1.8px 3px rgba(0, 0, 0, 0.5), inset -1.8px -
2.8px 1.8px -1.8px rgba(0, 0, 0, 0.5), inset 2.3px 2.8px 1.2px -1.2px rgba(255,
230, 80, 1)";
daytimeBackground[0].style.transform = "translateX(0)";
daytimeBackground[1].style.transform = "translateX(0)";
daytimeBackground[2].style.transform = "translateX(0)";
cloud.style.transform = "translateY(7px)";
cloudLight.style.transform = "translateY(7px)";
components.style.backgroundColor = "rgba(70, 133, 192,1)";
moon[0].style.opacity = "0";
moon[1].style.opacity = "0";
moon[2].style.opacity = "0";
stars.style.transform = "translateY(-80px)";
stars.style.opacity = "0";
changeTheme("light");
} else { // 當前為白天模式,切換為暗黑模式
body.classList.add('dark-mode');
mainButton.style.transform = "translateX(72px)";
mainButton.style.backgroundColor = "rgba(195, 200,210,1)";
mainButton.style.boxShadow =
"1.8px 1.8px 3px rgba(0, 0, 0, 0.5), inset -1.8px -
2.8px 1.8px -1.8px rgba(0, 0, 0, 0.5), inset 2.3px 2.8px 1.2px -1.2px rgba(255,
255, 210, 1)";
daytimeBackground[0].style.transform = "translateX(72px)";
daytimeBackground[1].style.transform = "translateX(55px)";
daytimeBackground[2].style.transform = "translateX(35px)";
cloud.style.transform = "translateY(50px)";
cloudLight.style.transform = "translateY(50px)";
components.style.backgroundColor = "rgba(25,30,50,1)";
moon[0].style.opacity = "1";
moon[1].style.opacity = "1";
moon[2].style.opacity = "1";
stars.style.transform = "translateY(-40px)";
stars.style.opacity = "1";
changeTheme("dark");
}
isClicked = true;
setTimeout(function () {
isClicked = false;
}, 500);
isMoved = !isMoved;
};
if (isMoved) {
mainButton.style.transform = "translateX(67px)";
daytimeBackground[0].style.transform = "translateX(67px)";
daytimeBackground[1].style.transform = "translateX(50px)";
daytimeBackground[2].style.transform = "translateX(30px)";
star[0].style.top = "7px";
star[0].style.left = "24px";
star[1].style.top = "24px";
star[1].style.left = "58px";
star[2].style.top = "16px";
star[2].style.left = "12px";
star[3].style.top = "23px";
star[3].style.left = "40px";
star[4].style.top = "12px";
star[4].style.left = "46px";
star[5].style.top = "31px";
star[5].style.left = "23px";
} else {
mainButton.style.transform = "translateX(5px)";
daytimeBackground[0].style.transform = "translateX(5px)";
daytimeBackground[1].style.transform = "translateX(3px)";
daytimeBackground[2].style.transform = "translateX(2px)";
cloudList[0].style.right = "-15px";
cloudList[0].style.bottom = "7px";
cloudList[1].style.right = "-8px";
cloudList[1].style.bottom = "-19px";
cloudList[2].style.right = "13px";
cloudList[2].style.bottom = "-27px";
cloudList[3].style.right = "30px";
cloudList[3].style.bottom = "-25px";
cloudList[4].style.right = "44px";
cloudList[4].style.bottom = "-42px";
cloudList[5].style.right = "66px";
cloudList[5].style.bottom = "-34px";
cloudList[6].style.right = "-15px";
cloudList[6].style.bottom = "7px";
cloudList[7].style.right = "-8px";
cloudList[7].style.bottom = "-19px";
cloudList[8].style.right = "13px";
cloudList[8].style.bottom = "-27px";
cloudList[9].style.right = "30px";
cloudList[9].style.bottom = "-25px";
cloudList[10].style.right = "44px";
cloudList[10].style.bottom = "-42px";
cloudList[11].style.right = "66px";
cloudList[11].style.bottom = "-34px";
}
});
star[0].style.top = "7px";
star[0].style.left = "24px";
star[1].style.top = "24px";
star[1].style.left = "58px";
star[2].style.top = "16px";
star[2].style.left = "12px";
star[3].style.top = "23px";
star[3].style.left = "40px";
star[4].style.top = "12px";
star[4].style.left = "46px";
star[5].style.top = "31px";
star[5].style.left = "23px";
} else {
mainButton.style.transform = "translateX(0px)";
daytimeBackground[0].style.transform = "translateX(0px)";
daytimeBackground[1].style.transform = "translateX(0px)";
daytimeBackground[2].style.transform = "translateX(0px)";
cloudList[0].style.right = "-14px";
cloudList[0].style.bottom = "7px";
cloudList[1].style.right = "-7px";
cloudList[1].style.bottom = "-18px";
cloudList[2].style.right = "14px";
cloudList[2].style.bottom = "-26px";
cloudList[3].style.right = "32px";
cloudList[3].style.bottom = "-24px";
cloudList[4].style.right = "46px";
cloudList[4].style.bottom = "-40px";
cloudList[5].style.right = "68px";
cloudList[5].style.bottom = "-32px";
cloudList[6].style.right = "-14px";
cloudList[6].style.bottom = "7px";
cloudList[7].style.right = "-7px";
cloudList[7].style.bottom = "-18px";
cloudList[8].style.right = "14px";
cloudList[8].style.bottom = "-26px";
cloudList[9].style.right = "32px";
cloudList[9].style.bottom = "-24px";
cloudList[10].style.right = "46px";
cloudList[10].style.bottom = "-40px";
cloudList[11].style.right = "68px";
cloudList[11].style.bottom = "-32px";
}
});
const initTheme =
document.querySelector('.components').getAttribute('data-value') || 'light';
const changeTheme = (detail) => {
// 你可以在这里添加你想在主题更改时执行的代码
// 默认什么都不做
console.log("Theme changed to:", detail)
}
const root = document.querySelector('.container')
func(root, initTheme, changeTheme);
})();
</script>
</div>