简体   繁体   English

我有一个可切换网站主题的按钮,如果再次单击以更改回默认主题,该如何做相同的按钮?

[英]I have a button that switches the theme of my website how do i make the same button if clicked again to change back to the default Theme?

When the theme changer button is clicked it changes the colors of my website how do i make it so when the user clicks the button again it changes back to the original colors. 单击主题更改器按钮后,它会更改我网站的颜色,我该如何更改颜色,以便当用户再次单击该按钮时,它会变回原始颜色。

JS code: JS代码:

$(document).ready(function () {
    $('#ChangeTheme').click(function () {
        document.body.style.setProperty("--color1", "orange")
        document.body.style.setProperty("--color2", "red")
        document.body.style.setProperty("--color3", "white")
        return false;
    });
});

CSS code: CSS代码:

:root {
    --color1: #3366cc;
    --color2: #2d2d2d;
    --color3: white;
}

#header {
    background-color: #3366cc;
    height: 110px;
    position: fixed;
    top: 0;
    width: 100%;
    display: table;
    border-bottom: 5px solid #0099ff;
    border-top: 5px solid #2d2d2d;
    background: linear-gradient(var(--color2), var(--color1));
}

#footer {
    background-color: darkblue;
    height: 150px;
    position: fixed;
    bottom: 0;
    width: 100%;
    border-top: 5px solid #0099ff;
    background: linear-gradient(var(--color1), var(--color2));
}

HTML code: HTML代码:

      <asp:Button ID="ChangeTheme" runat="server" Text="Change Theme" Width="110px" CausesValidation="false" BackColor="#99CCFF" BorderColor="#000066" BorderStyle="Solid" />

You could use an if statement, and check what is the --color1 value, for example: 您可以使用if语句,并检查--color1值是什么,例如:

    jQuery(document).ready(function ($) {
      $('#ChangeTheme').click(
        function(){
          if($("body").css("--color1")!=="orange"){
            document.body.style.setProperty("--color1", "orange");
            document.body.style.setProperty("--color2", "red");
            document.body.style.setProperty("--color3", "white");
          } else {
            document.body.style.setProperty("--color1", "#3366cc");
            document.body.style.setProperty("--color2", "#2d2d2d");
            document.body.style.setProperty("--color3", "white");
          }
        }
      );
    });

You can try 你可以试试

document.body.classList.toggle('nameOfClass') //to toggle document.body.classList.toggle('nameOfClass')//切换

document.body.classList.add('nameOfClass') //to add document.body.classList.add('nameOfClass')//要添加

document.body.classList.remove('nameOfClass') // to remove document.body.classList.remove('nameOfClass')//删除

You can do this with just css and js, keep all your classes with the default color, with or without variables, and create a named class that you can apply to the specific elements. 您可以仅使用css和js来执行此操作,将所有类都保留为默认颜色(带或不带变量),并创建可以应用于特定元素的命名类。

:root {
    --color1: #3366cc;
    --color2: #2d2d2d;
    --color3: white;
}

#header {
    background-color: #3366cc;
    height: 110px;
    position: fixed;
    top: 0;
    width: 100%;
    display: table;
    border-bottom: 5px solid #0099ff;
    border-top: 5px solid #2d2d2d;
    background: linear-gradient(var(--color2), var(--color1));
}

#footer {
    background-color: darkblue;
    height: 150px;
    position: fixed;
    bottom: 0;
    width: 100%;
    border-top: 5px solid #0099ff;
    background: linear-gradient(var(--color1), var(--color2));
}

#header.theme2 {
    background: linear-gradient(red, orange);
}
#footer.theme2 {
    background: linear-gradient(orange, red);
}

Your js code can look like this then: 您的js代码如下所示:

function toggleTheme2(){
    ['#header', '#footer'].forEach(x=>document.querySelectorAll(x).forEach(x=>x.classList.toggle('theme2')))
}

and the html button: 和html按钮:

<asp:Button ID="ChangeTheme" runat="server" onclick="toggleTheme2" Text="Change Theme" Width="110px" CausesValidation="false" BackColor="#99CCFF" BorderColor="#000066" BorderStyle="Solid" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的网站上有一个切换主题按钮,我如何在刷新或转到其他页面时保存主题? - I have a switch theme button, on my website, how can i save the theme when refreshing, or going to an other pages? 如何使主题按钮更改器影响网站上的所有页面而不仅仅是主页? - How do i make my theme button changer affect all pages on my site not just the homepage? 如何使用主题更改 MUI 中按钮的形状? - How do I change the shape of a button in MUI using theme? 如何用Vue.js制作一个切换主题的按钮? - How to make a button that switches the theme with Vue.js? 如何让我的按钮清除前两个表单然后再次单击清除最后一个表单? - How do I make my button clear the first two forms then when clicked again clear the last form? 为什么我必须调用$(“:button”)。button(); 启用JQuery UI主题? - Why do I have to call $(“:button”).button(); to enable JQuery UI Theme? 单击按钮时更改响应式网站的主题 - change theme of the responsive website on button click 单击按钮时如何更改按钮的颜色并在再次单击按钮时将其更改回原始颜色? - How to change a button's color when clicked on it and change it back to its original color when the button is clicked again? 我在网站上有一个返回页面顶部的按钮,如何动画化返回顶部的动画? - I have a return to top of the page button in my website, how can I animate scrolling back to the top? 如何在单击时更改跨度文本的颜色,并在再次单击时恢复正常? - How do I change the color of the span text when it's clicked, and back to normal when clicked again?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM