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

标签 javascript jquery html css

我有一个开关,希望一个显示为关闭(灰色),一个显示为打开(蓝色),默认为打开。在这种特定情况下,我希望第一行具有蓝色“开启”元素,第二行和第三行将切换开关显示为关闭或灰色。

我已经尝试在 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/

相关文章:

html - 不以 "r"结尾的类属性

javascript - javascript 中的 "onclick"和 "this"

javascript - 如何从两个表中获取firebase的所有子数据

javascript - 按图像过滤 不想显示所有图像

javascript - 基于javascript变量渲染jsp内容

javascript - 根据国家/地区语言填充innerHTML 的JavaScript 函数

javascript - 单击表行上的按钮并在模式窗口中显示值

javascript - 鼠标悬停时动画元素向下滑动

javascript - 尝试从文本框中获取数据并将其插入数据库

javascript - 如何使用 jQuery 将双引号内的破折号 html 元素作为目标?