jQuery暗模式切换仅工作一半

时间:2019-06-11 19:13:22

标签: jquery html css

为任务创建暗模式切换,但仅适用于50%

创建了2个可以更改主体背景和文本颜色的类。

在暗模式下切换为ON可以正常工作,将其切换回亮模式似乎不起作用。出于某种原因,似乎无法删除“深色”类,而改为了“浅色”

$('.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>

1 个答案:

答案 0 :(得分:2)

您始终可以简化代码,将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>