简体   繁体   English

如何有一个开关,同时显示一开一关?

[英]How to have an on off switch, display one on and one off at the same time?

I have an on-off switch and would like for one to display as off (grey color) and one to display as on (blue color), with on being the default.我有一个开关,希望一个显示为关闭(灰色),一个显示为打开(蓝色),默认为打开。 In this specific case, I want the first row to have blue "on" elements, with the second and third-row displaying the toggle switch as off or grey.在这种特定情况下,我希望第一行具有蓝色“打开”元素,第二行和第三行将切换开关显示为关闭或灰色。

I've tried adding additional elements in the css but nothing has worked so far.我试过在 css 中添加额外的元素,但到目前为止没有任何效果。

 .switch { position: relative; display: inline-block; width: 38px; height: 22px; } .switch input { display: none; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #98B3DD; -webkit-transition: .4s; transition: .4s; } .slider:before { position: absolute; content: ""; height: 16px; width: 16px; left: 3px; bottom: 3px; background-color: white; -webkit-transition: .4s; transition: .4s; } input:focus+.slider { box-shadow: 0 0 1px rgb(221, 223, 235); } .slider.round { border-radius: 34px; } .slider.round:before { border-radius: 50%; }
 <ul class="list-group list-group-flush"></ul> <div class="row"> <div class="col-xl-11"> <div class="table-responsive"> <table class="table"> <thead> <tr></tr> </thead> <tbody> <tr> <td>Device 1</td> <td><i class="far fa-edit fa-2x text-#98B3DD" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i></td> <td><i class="far fa-times-circle fa-2x text-#98B3DD" style="font-size: 25px;"></i></td> <td><label class="switch"><input type="checkbox" /><span class="slider round"></span></label></td> </tr> <tr> <td>Device 2</td> <td><i class="far fa-edit fa-2x text-gray-300" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i></td> <td><i class="far fa-times-circle fa-2x text-gray-300" style="font-size: 25px;"></i></td> <td><label class="switch"><input type="checkbox" /><span class="slider round"></span></label></td> </tr> <tr> <td>Device 3</td> <td><i class="far fa-edit fa-2x text-gray-300" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i></td> <td><i class="far fa-times-circle fa-2x text-gray-300" style="font-size: 25px;"></i></td> <td><label class="switch"><input type="checkbox" /><span class="slider round"></span></label></td> </tr> </tbody> </table> </div> </div> </div> <div class="row"> <div class="col-xl-11"> <div><button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#addnewdevice">Add New Device</button> <div class="collapse" id="addnewdevice">

I think I have found a solution, the comments in the code explain the changes I have made.我想我已经找到了解决方案,代码中的注释解释了我所做的更改。

 .switch { position: relative; display: inline-block; width: 38px; height: 22px; } .switch input { display:none; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #98B3DD; -webkit-transition: .4s; transition: .4s; } .slider:before { position: absolute; content: ""; height: 16px; width: 16px; left: 3px; bottom: 3px; background-color: white; -webkit-transition: .4s; transition: .4s; } input:focus + .slider { box-shadow: 0 0 1px rgb(221, 223, 235); } .slider.round { border-radius: 34px; } .slider.round:before { border-radius: 50%; } /* ADDED THIS BELOW */ input:checked + .slider:before { -webkit-transform: translateX(16px); -ms-transform: translateX(16px); transform: translateX(16px); } input:checked + .slider { background-color: blue; }
 <!-- In the input tag I added the word *checked* for Device 1 and left it out for Device 2 and Device 3 --> <ul class="list-group list-group-flush"></ul> <div class="row"> <div class="col-xl-11"> <div class="table-responsive"> <table class="table"> <thead> <tr></tr> </thead> <tbody> <tr> <td>Device 1</td> <td><i class="far fa-edit fa-2x text-#98B3DD" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i></td> <td><i class="far fa-times-circle fa-2x text-#98B3DD" style="font-size: 25px;"></i></td> <td><label class="switch"><input checked type="checkbox" /><span class="slider round"></span></label></td> </tr> <tr> <td>Device 2</td> <td><i class="far fa-edit fa-2x text-#98B3DD" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i></td> <td><i class="far fa-times-circle fa-2x text-#98B3DD" style="font-size: 25px;"></i></td> <td><label class="switch"><input type="checkbox" /><span class="slider round"></span></label></td> </tr> <tr> <td>Device 3</td> <td><i class="far fa-edit fa-2x text-#98B3DD" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i></td> <td><i class="far fa-times-circle fa-2x text-#98B3DD" style="font-size: 25px;"></i></td> <td><label class="switch"><input type="checkbox" /><span class="slider round"></span></label></td> </tr> </tbody> </table> </div> </div> </div> <div class="row"> <div class="col-xl-11"> <div><button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#addnewdevice">Add New Device</button> <div class="collapse" id="addnewdevice">

I have added two css classes (.switch-on and .switch-off) to display switch in on and off state.我添加了两个 css 类(.switch-on 和 .switch-off)来显示开关处于打开和关闭状态。 Is this something like what you want ?这是你想要的吗?

 .switch { position: relative; display: inline-block; width: 38px; height: 22px; } .switch input { display: none; } .slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; -webkit-transition: 0.4s; transition: 0.4s; } .slider:before { position: absolute; content: ""; height: 16px; width: 16px; bottom: 3px; background-color: white; -webkit-transition: 0.4s; transition: 0.4s; } .slider-on { background-color: #98b3dd; } .slider-off { background-color: #ccc; } .slider-on::before { left: 3px; } .slider-off::before { right: 3px; } input:focus + .slider { box-shadow: 0 0 1px rgb(221, 223, 235); } .slider.round { border-radius: 34px; } .slider.round:before { border-radius: 50%; }
 <div class="row"> <div class="col-xl-11"> <div class="table-responsive"> <table class="table"> <thead> <tr></tr> </thead> <tbody> <tr> <td>Device 1</td> <td> <i class="far fa-edit fa-2x text-#98B3DD" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i> </td> <td> <i class="far fa-times-circle fa-2x text-#98B3DD" style="font-size: 25px;"></i> </td> <td> <label class="switch"><input type="checkbox"/><span class="slider slider-on round" ></span ></label> </td> </tr> <tr> <td>Device 2</td> <td> <i class="far fa-edit fa-2x text-gray-300" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i> </td> <td> <i class="far fa-times-circle fa-2x text-gray-300" style="font-size: 25px;"></i> </td> <td> <label class="switch"><input type="checkbox"/><span class="slider slider-off round" ></span ></label> </td> </tr> <tr> <td>Device 3</td> <td> <i class="far fa-edit fa-2x text-gray-300" style="font-size: 25px;height: 22px;margin-left: 2px;margin-right: 2px;"></i> </td> <td> <i class="far fa-times-circle fa-2x text-gray-300" style="font-size: 25px;"></i> </td> <td> <label class="switch"><input type="checkbox"/><span class="slider slider-off round" ></span ></label> </td> </tr> </tbody> </table> </div> </div> </div>

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

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