为作业创建暗模式切换,但它仅适用于 50%
创建了 2 个类,这些类应该更改正文的背景 + 文本颜色。
打开暗模式工作正常,将其切换为返回浅色模式似乎不起作用。由于某种原因似乎无法删除"深色"类并更改为"浅色"类
$('.switch').click(function() {
if ($('body').not('dark')) {
$('body').addClass('dark');
}
else if ($('body').hasClass('dark')) {
$('body').removeClass('dark');
$('body').addClass('light');
}
});
.dark {
background-color: #000000;
transition: .5s all ease;
color: #ffffff;
}
.light {
background-color: #ffffff;
transition: .5s all ease;
color: #000000;
}
/* ----------------------------- SLIDER -----------------------------*/
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
/* float: right;*/
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider::before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #696969;
}
/*
input:focus + .slider {
box-shadow: 0 0 1px #696969;
}
*/
input:checked + .slider::before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round::before {
border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p> Switch theme </p>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
您可以随时简化代码并使light
样式成为body
的默认值,然后只需打开dark
CHANGE
而不是CLICK
。
在开关上使用click
时,您实际上触发了两次更改功能。但是,处理它的正确方法就像一个复选框,它是并使用 change
.
此外,not('dark')
不起作用,因为dark
不是选择器,因此if
语句将永远不起作用。
$(document).on("change",".switch",function() {
$("body").toggleClass("dark");
});
body {
background-color: #ffffff;
transition: .5s all ease;
color: #000000;
}
body.dark {
background-color: #000000;
transition: .5s all ease;
color: #ffffff;
}
/* ----------------------------- SLIDER -----------------------------*/
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
/* float: right;*/
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider::before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #696969;
}
/*
input:focus + .slider {
box-shadow: 0 0 1px #696969;
}
*/
input:checked + .slider::before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round::before {
border-radius: 50%;
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<p> Switch theme </p>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</body>
</html>