如何确定'切换' CSS切换输入的状态?

时间:2016-10-29 09:31:32

标签: html css toggle

我想实现一个基于CSS的切换/切换输入。我发现以下链接的示例非常简单,根本不使用javascript。问题是它缺乏如何确定“切换”的解释。状态

http://www.w3schools.com/howto/howto_css_switch.asp

这是完整的参考代码:



/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */
.switch input {display:none;}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

<!-- Rounded switch -->
<label class="switch">
  <input type="checkbox">
  <div class="slider"></div>
</label>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

如果html是这样的

<label  class="switch">
    <input id="showflagsswitch" onclick="showFlags()" type="checkbox">
    <span class="slider round"></span>
  </label>

然后此javascript将为您提供价值

switchFlags=document.getElementById("showflagsswitch").checked;

答案 1 :(得分:0)

出于某种原因,我没有尝试过简单的解决方案..现在它正在运行:)

document.getElementById("input").checked