更改切换按钮样式

时间:2018-11-26 01:14:44

标签: css css3 sass css-selectors html5-canvas

我想这样更改切换按钮的样式。

Toggle-Button

这是我的示例代码:

<div class="form-group has-feedback">
  <input id="wffm302ebacf2b634d59b0dc1e81e451bc8d_Sections_0__Fields_6__Id" name="wffm302ebacf2b634d59b0dc1e81e451bc8d.Sections[0].Fields[6].Id" type="hidden" value="{D7AB5D2E-444F-49C7-91E4-564496D7C8A2}">
  <div>
    <input data-val="true" data-val-required="The Value field is required." id="wffm302ebacf2b634d59b0dc1e81e451bc8d_Sections_0__Fields_6__Value" name="wffm302ebacf2b634d59b0dc1e81e451bc8d.Sections[0].Fields[6].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>
    <label class="switch">
      <input name="wffm302ebacf2b634d59b0dc1e81e451bc8d.Sections[0].Fields[6].Value" type="checkbox" value="false">
      <span class="slider round"></span>
    </label>
  </div>
  <span class="field-validation-valid help-block" data-valmsg-for="wffm302ebacf2b634d59b0dc1e81e451bc8d.Sections[0].Fields[6].Value" data-valmsg-replace="true"></span>
</div>

Jsfiddle example

我尝试了一些CSS,但这并不是我想要的。我尝试过:

  .switchbtn:before {
    position: absolute;
    content: "";
    height: 32px;
    width: 26px;            
    background-color: black;
    -webkit-transition: .4s;
    transition: .4s;
}

1 个答案:

答案 0 :(得分:4)

您处在正确的轨道上。您可以使用:before:after伪CSS选择器来修改切换按钮的外观。由于:before已用于创建循环切换,因此我已使用:after添加了文本。显然,有更多的方法可以做到这一点,下面是向您展示如何实现此目标的快速方法。

请注意,您需要为两种状态(正常和checked)修改伪选择器,因为您需要不同的颜色。

检查下面的代码片段,并以此为起点根据需要修改/调整样式。

希望这会有所帮助。

  .switch {
    position: relative;
    display: inline-block;
    width: 60px;
    height: 34px;
  }

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

  .switchbtn {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    border: 2px solid #bbb;

    -webkit-transition: .4s;
    transition: .4s;
  }

 .switchbtn:before {
    position: absolute;
    content: "";
    height: 34px;
    width: 34px;
    left: -2px;
    top: -2px;
    background-color: black;
    -webkit-transition: .4s;
    transition: .4s;
  
  }
  
 .switchbtn:after {
    position: absolute;
    content: "OFF";
    right: 3px;
    top: 10px;
    font-size: 9px;
    font-family: 'Arial';
    -webkit-transition: .4s;
    transition: .4s;
  }

  input:checked+.switchbtn {
    background-color: white;
    border: 2px solid #2196F3;
  }

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

  input:checked+.switchbtn:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(26px);
    background-color: #2196F3;
  }
  
  input:checked+.switchbtn:after {
    position: absolute;
    content: "ON";
    left: 7px;
    top: 10px;
    font-size: 9px;
    font-family: 'Arial';
    -webkit-transition: .4s;
    transition: .4s;
  }

  /* Rounded sliders */

  .switchbtn {
    border-radius: 34px;
  }

  .switchbtn:before {
    border-radius: 50%;
  }

  .switch button {
    display: none;
  }
<label class="switch">
    <input class="switchbtn" data-val="true" data-val-required="The Value field is required." id="wffm302ebacf2b634d59b0dc1e81e451bc8d_Sections_0__Fields_6__Value" name="wffm302ebacf2b634d59b0dc1e81e451bc8d.Sections[0].Fields[6].Value" type="checkbox" value="true" autocomplete="off" style="display: none;" aria-invalid="false" aria-describedby="wffm302ebacf2b634d59b0dc1e81e451bc8d\.Sections\[0\]\.Fields\[6\]\.Value-error wffm302ebacf2b634d59b0dc1e81e451bc8d\.Sections\[0\]\.Fields\[6\]\.Value-error">
    <span class="button-checkbox bootstrap-checkbox switchbtn">
    <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" style="display: inline;"></span>
    <span class="icon cb-icon-check-indeterminate" style="display:none;"></span>
    </button></span><input name="wffm302ebacf2b634d59b0dc1e81e451bc8d.Sections[0].Fields[6].Value" type="hidden" value="false">
</label>

希望这会有所帮助。

相关问题