繁体   English   中英

JavaScript:将CSS切换到暗模式

[英]JavaScript: css toggle switch to dark mode

我已经使用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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM