我有一个开关,希望一个显示为关闭(灰色),一个显示为打开(蓝色),默认为打开。在这种特定情况下,我希望第一行具有蓝色“开启”元素,第二行和第三行将切换开关显示为关闭或灰色。
我已经尝试在 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">
最佳答案
我想我找到了解决方案,代码中的注释解释了我所做的更改。
.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">
关于javascript - 如何有一个开关,同时显示一开一关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57538731/