JavaScript:css 切换到暗模式

标签 javascript jquery html css toggle

我使用 CSS 创建了一个切换开关。

我希望 #box1 从黑色字体的白色背景变为黑色白色文本,并将开关切换为开启 .

我一直在尝试找出 JavaScript。按钮没有切换,框也没有变回白色。

$('#myonoffswitch').change(function(){   
    console.log('here');
    if ($(this).click())
    {
        $('#box1').addClass('dark-mode');
    }
    else
    {
        $('#box1').removeClass('dark-mode');
    }
});

如何切换 dark-mode 类。

Fiddle

最佳答案

您可以只使用 toggleClass方法。 Demo

$('#myonoffswitch').change(function(){   
    $('#box1').toggleClass('dark-mode');    
});

$(this).click() 返回一个 jQuery 的实例,它在 JavaScript 中是一个真值,这就是为什么首先调用 addClass 的原因

$('#myonoffswitch').change(function() {
  $('#box1').toggleClass('dark-mode');
});
.onoffswitch {
  position: relative;
  width: 90px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}
.onoffswitch-checkbox {
  display: none;
}
.onoffswitch-label {
  display: block;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid #999999;
  border-radius: 20px;
}
.onoffswitch-inner {
  display: block;
  width: 200%;
  margin-left: -100%;
  -moz-transition: margin 0.3s ease-in 0s;
  -webkit-transition: margin 0.3s ease-in 0s;
  -o-transition: margin 0.3s ease-in 0s;
  transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before,
.onoffswitch-inner:after {
  display: block;
  float: left;
  width: 50%;
  height: 30px;
  padding: 0;
  line-height: 30px;
  font-size: 14px;
  color: white;
  font-family: Trebuchet, Arial, sans-serif;
  font-weight: bold;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
.onoffswitch-inner:before {
  content: "ON";
  padding-left: 10px;
  background-color: #FFFFFF;
  color: #000000;
}
.onoffswitch-inner:after {
  content: "OFF";
  padding-right: 10px;
  background-color: #FFFFFF;
  color: #000000;
  text-align: right;
}
.onoffswitch-switch {
  display: block;
  width: 18px;
  margin: 6px;
  background: #000000;
  border: 2px solid #999999;
  border-radius: 20px;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 56px;
  -moz-transition: all 0.3s ease-in 0s;
  -webkit-transition: all 0.3s ease-in 0s;
  -o-transition: all 0.3s ease-in 0s;
  transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
  margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
  right: 0px;
  background-color: #FFFFFF;
}
#box1 {
  color: black;
  background-color: white;
  border: 1px solid;
  width: 100px;
  height: 100px;
}
#box1.dark-mode {
  color: #fff;
  background-color: #000;
  width: 100px;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="onoffswitch">
  <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
  <label class="onoffswitch-label" for="myonoffswitch">
    <span class="onoffswitch-inner"></span>
    <span class="onoffswitch-switch"></span>
  </label>
</div>
<br>
<div id="box1">This is text.</div>

关于JavaScript:css 切换到暗模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29054171/

相关文章:

javascript - ES6 是否引入了一种机制来创建 block 作用域函数语句(而不是表达式)?

javascript - 用 Jest 模拟第 3 方库构造函数

javascript - CoffeeScript 中的回调

javascript - 两个选择表列

jquery - 奇怪的 jQuery 问题 - 对 C 程序的 Ajax 请求不太工作

html - 检查数组值是否包含html中的特定值

javascript - YouTube iframe Embed 无法控制 iPad 上的音频

javascript - Node对象和Element对象的区别?

javascript - 如何在鼠标悬停时显示横幅 div?

javascript - 对话框未打开 ajax jquery MVC2 asp.net