复选框切换开关

时间:2018-11-21 03:55:39

标签: javascript css html5 css3 toggle

我有一个复选框,需要将其转换为拨动开关

下面的HTML是一个复选框

https://jsfiddle.net/kdej9w2y/2/

   <div class="checkbox">
        <label>
            <input data-val="true" data-val-required="The Value field is required." id="wffm302ebacf2b634d59b0dc1e81e451bc8d_Sections_0__Fields_7__Value" name="wffm302ebacf2b634d59b0dc1e81e451bc8d.Sections[0].Fields[7].Value" type="checkbox" value="true" autocomplete="off" style="display: none;">
            <span class="button-checkbox bootstrap-checkbox">
            <button type="button" class="btn clearfix custom-btn">
            <span class="icon fa fa-check theme-text" style="display:none;"></span><span class="icon fa fa-check-square">

            </span>
            <span class="icon cb-icon-check-indeterminate" style="display:none;"></span>
            </button>
            </span>
            <input name="wffm302ebacf2b634d59b0dc1e81e451bc8d.Sections[0].Fields[7].Value" type="hidden" value="false">
            Flag
        </label>
    </div>

我尝试应用不同的CSS,但是它什么也没做。 HTML始终与我的scnerio中的一样,因此是否可以在当前复选框HTML上实现切换开关。

3 个答案:

答案 0 :(得分:0)

尝试一下

为此Switch添加HTML

<!-- Rectangular switch -->
<label class="switch">
  <input type="checkbox">
  <span class="slider"></span>
</label>

<!-- Rounded switch -->
<label class="switch">
 <input type="checkbox">
 <span class="slider round"></span>
</label>

然后为开关添加此CSS语句

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

/* Hide default HTML checkbox */
  .switch input {
    opacity: 0;
    width: 0;
    height: 0;
  }

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

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

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 sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

答案 1 :(得分:0)

  

首先将此css放入代码中

<style>
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}
.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}
.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

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 sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

  

删除多余的代码,并将类滑子回合放入span标签

<label class="switch">
  <input data-val="true" id="wfe" name="chek" type="checkbox" value="true" autocomplete="off"/>
  <span class="slider round"></span>
  </label>

答案 2 :(得分:0)

您可以尝试一下。

.switch {
    position: relative;
    display: inline-block;
    width: 45px;
    height: 17px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    -webkit-transition: .4s;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 15px;
    width: 15px;
    left: 2px;
    bottom: 1px;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
}

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

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

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

.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}
<label class="switch">
  <input name="status" class="status" type="checkbox" checked="">
<div class="slider round"></div></label>