[英]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.