JavaScript:将CSS切换到暗模式

用户名

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

我想#box1从一个白色背景黑色字体与白色文本和转向拨动黑色转

我一直试图找出JavaScript。按钮不会切换,并且框也不会变回白色。

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

如何切换dark-mode课程。

小提琴

罗布米

您可以只使用toggleClass方法。演示版

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JavaScript:将CSS切换到暗模式

来自分类Dev

如何使用 GNOME 在 Ubuntu 中快速切换到暗模式并使其完全暗?

来自分类Dev

将交换链切换到窗口模式

来自分类Dev

将任务列表切换到评论模式

来自分类Dev

暗模式切换

来自分类Dev

如何在Flutter中切换到暗/亮模式按钮?

来自分类Dev

在切换到亮或暗模式之前,SwiftUI View不会显示数据

来自分类Dev

从用户模式切换到内核模式

来自分类Dev

从用户模式切换到内核模式

来自分类Dev

React不会切换到生产模式

来自分类Dev

Notepad ++切换到奇怪的编辑模式

来自分类Dev

在Firemonkey中切换到发布模式

来自分类Dev

Chrome突然切换到“夜间模式”

来自分类Dev

如何切换到桌面模式

来自分类Dev

SELinux不会切换到“强制”模式

来自分类Dev

iOS通过代码将默认键盘从ABC模式切换到123模式?

来自分类Dev

iOS是否通过代码将默认键盘从ABC模式切换到123模式?

来自分类Dev

如何通过将手机切换为横向模式而不影响流视频来切换到全屏

来自分类Dev

使用按钮将网站切换到永久性全屏模式

来自分类Dev

如何将全屏Swing框架切换到全屏模式?

来自分类Dev

将Inno Setup许可证备忘录切换到RTL模式

来自分类Dev

如何在控制台模式下将功能键切换到不同的终端?

来自分类Dev

如何将 RN4871 / RN4870 蓝牙模块切换到命令模式?

来自分类Dev

将CIFilters切换到视频

来自分类Dev

将列值切换到左侧

来自分类Dev

将数据从数组切换到向量

来自分类Dev

将 Joomla 切换到 Wordpress

来自分类Dev

在JavaScript中从输入切换到按钮

来自分类Dev

用户模式下的进程切换到内核模式。那么该进程将具有root特权?

Related 相关文章

热门标签

归档