css 黑暗模式和谷歌MapCookie

ovfsdjhp  于 2023-01-10  发布在  其他
关注(0)|答案(4)|浏览(159)

我有两个主题:白色模式(默认)和黑暗模式,当我重新加载页面/加载另一个页面时,它会切换回默认主题,也会更改HTML中的一些图像。我如何制作cookie来解决这个问题?

var icon = document.getElementById("icon");

icon.onclick = function () {

document.body.classList.toggle("dark-theme");
if(  document.body.classList.contains("dark-theme")) {
    
    icon.src = "assets/white.svg";
    mis.src = "assets/misiune-b.svg"
    pnras.src = "assets/pnras-b.svg"
    bulb.src = "assets/white.svg"
    cam.src = "assets/cam-b.svg"
    man.src = "assets/man-b.svg"
    resurse.src = "assets/resurse-b.svg"
    consiliu.src = "assets/cladire-b.svg"
    dec.src = "assets/moneda-b.svg"
    pho.src = "assets/phone-b.svg"
    mail.src = "assets/mail-b.svg"
}
else {
    icon.src = "assets/moon.png"
    mis.src = "assets/misiune.svg"
    pnras.src = "assets/pnras.svg"
    bulb.src = "assets/.svg"
    cam.src = "assets/camera_.svg"
    man.src = "assets/management_.svg"
    resurse.src = "assets/resurse umane.svg"
    consiliu.src = "assets/ca.svg"
    dec.src = "assets/moneda.svg"
    pho.src = "assets/Phone.svg"
    mail.src = "assets/Mail.svg"
}

}

另外,我遇到的另一个问题是,当我包含GoogleMap时,我收到以下消息:common.js:50 GET https://maps.googleapis.com/maps/api/js/QuotaService.RecordEvent?1shttps%3A%2F%2Fwww.google.com%2Fmaps%2Fembed&2sgoogle-maps-embed&7snq9kpa&10e1&11b0&callback=xdc._9jkh3h&client=google-maps-embed&token=75745 net::ERR_BLOCKED_BY_CLIENT

okxuctiv

okxuctiv1#

使用css变量管理颜色和图标路径

6tqwzwtp

6tqwzwtp2#

你提供的代码只能在你当前使用的HTML上运行。通过改变页面,重新编码或关闭,你重置了所有的变量和脚本。这里的技巧是使用本地存储,存储变量和访问它们,即使在改变页面,重新加载或关闭。
最简单的答案是指向this nice教程,其中对所有内容进行了解释

7d7tgy0s

7d7tgy0s3#

你需要把主题保存在某个地方。你可以把它保存到JS中的一个cookie中,比如,当图标被点击时,创建一个selectedTheme cookie...
document.cookie = "selectedTheme=dark";
然后在JS中,你可以说“如果cookie === 'dark',则将dark模式类添加到body标记”。
或者将设置保存到Database中,并对body标记执行类似的if语句。

4smxwvx5

4smxwvx54#

在你编辑之前,我写了这个,我希望这能有所帮助.很多用户不接受cookies ...所以:

let darkMode = false;
        let myContent = null;
        let mySwitch = null;
        let display = null;
        document.addEventListener("DOMContentLoaded", onDomContentLoaded);
        function onDomContentLoaded(e){
            myContent = document.getElementById("myDiv");
            mySwitch = document.getElementById("switcher");
            display = document.getElementById("darkValue");
            display.textContent = "dark mode = " + darkMode;
            mySwitch.addEventListener("click",clickHandler);
        }
        function clickHandler(e){
            darkMode = !darkMode;
            
            if(darkMode==true){
                myContent.className = "black";
                display.textContent = "dark mode = " + darkMode;
            }
            else if(darkMode==false){
                myContent.className = "white";
                display.textContent = "dark mode = " + darkMode;
            }
        }
.white{
            background-color: beige;
            color: grey;
        }
        .black{
            background-color:darkslategrey;
            color: beige;
        }
<div id="myDiv" class="white">
            <p>My text</p>
            <p id="darkValue"></p>
            <button id="switcher">switcher</button>
        </div>

相关问题