Dark mode gets turned off on refresh a page.

huangapple go评论73阅读模式
英文:

Dark mode gets turned off on refresh a page

问题

My page not load dark mode when it refresh.

const dayNight = document.querySelector(".day-night");
dayNight.addEventListener("click", () => {
    dayNight.querySelector("i").classList.toggle("fa-sun");
    dayNight.querySelector("i").classList.toggle("fa-moon");
    document.body.classList.toggle("dark");
});
window.addEventListener("load", () => {
    if (document.body.classList.contains("dark")) {
        dayNight.querySelector("i").classList.add("fa-sun");
    } else {
        dayNight.querySelector("i").classList.add("fa-moon");
    }
})
英文:

my page not load darkmode when it refresh

const dayNight = document.querySelector(".day-night");
dayNight.addEventListener("click", () => {
    dayNight.querySelector("i").classList.toggle("fa-sun");
    dayNight.querySelector("i").classList.toggle("fa-moon");
    document.body.classList.toggle("dark");
});
window.addEventListener("load", () => {
    if (document.body.classList.contains("dark")) {
        dayNight.querySelector("i").classList.add("fa-sun");
    } else {
        dayNight.querySelector("i").classList.add("fa-moon");
    }
})

答案1

得分: 2

你可以使用LocalStorage来持久保存数据。

const dayNight = document.querySelector(".day-night");
dayNight.addEventListener("click", () => {
    dayNight.querySelector("i").classList.toggle("fa-sun");
    dayNight.querySelector("i").classList.toggle("fa-moon");
    const isDark = document.body.classList.toggle("dark");
    // 本地持久化
    localStorage.setItem("dark", isDark);
});
window.addEventListener("load", () => {
    const isDark = localStorage.getItem("dark");
    if (isDark === "true") {
        document.body.classList.add("dark");
        dayNight.querySelector("i").classList.add("fa-moon");
    } else {
        document.body.classList.remove("dark");
        dayNight.querySelector("i").classList.add("fa-sun");
    }
})

你还可以查看@media prefers-color-scheme css 媒体特性,以根据用户系统的首选项检测颜色模式。

英文:

You can use LocalStorage to persist the data.

const dayNight = document.querySelector(".day-night");
dayNight.addEventListener("click", () => {
    dayNight.querySelector("i").classList.toggle("fa-sun");
    dayNight.querySelector("i").classList.toggle("fa-moon");
    const isDark = document.body.classList.toggle("dark");
    // Persist this locally
    localStorage.setItem("dark", isDark);
});
window.addEventListener("load", () => {
    const isDark = localStorage.getItem("dark");
    if (isDark === "true") {
        document.body.classList.add("dark");
        dayNight.querySelector("i").classList.add("fa-moon");
    } else {
        document.body.classList.remove("dark");
        dayNight.querySelector("i").classList.add("fa-sun");
    }
})

You can also checkout @media prefers-color-scheme css media feature to detect it with respect to user's system preferences.

huangapple
  • 本文由 发表于 2023年6月29日 12:14:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定