【问题标题】:How to determine the 'toggle' status of a CSS toggle input?如何确定 CSS 切换输入的“切换”状态?
【发布时间】:2016-10-29 09:31:31
【问题描述】:

我想实现一个基于 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>

【问题讨论】:

    标签: html css toggle


    【解决方案1】:

    如果html是这样的

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

    那么这个 javascript 会给你价值

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

    【讨论】:

      【解决方案2】:

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

      document.getElementById("input").checked

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 2017-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多