CSS切换过渡动画

时间:2016-05-23 19:38:50

标签: html css animation toggle slide

我正在制作2个CSS切换按钮。现在当你点击按钮有淡入和淡出效果。我想更像是从右到左动画的滑动。随附的是HTML,CSS和简化我的代码的墨水。谢谢! https://jsfiddle.net/ebgpqvha/

<div class="switch-field">
  <div class="switch-title">Hand:</div>
  <input type="radio" id="switch_left" name="switch_2" value="yes" checked/>
  <label for="switch_left">L</label>
  <input type="radio" id="switch_right" name="switch_2" value="no" />
  <label for="switch_right">R</label>
</div>


       .switch-field, .switch-field2 {
      font-family: "adiHaus", Arial, sans-serif;
      font-size: 16px;
        font-weight: 300;
        margin: 0.2em;
        overflow: hidden;
    }       


    .switch-title {
      margin-bottom: 6px;
    }       

    .switch-field input, .switch-field2 input {
      display: none;
    }       


    .switch-field label, .switch-field2 label {
      float: left;
    }       


    .switch-field label {
      display: inline-block;
      width: 63px;
      height: 40px;
      background-color: #EFEFEF;
      color: #cdcdcd;
      font: 16px "adiHaus",Arial,sans-serif;
      font-weight: bold;
      text-align: center;
      text-shadow: none;
      padding: 10px 14px;
      border: 1px solid rgba(0, 0, 0, 0.2);
     border-top: #CCC solid 1px;
     border-radius: 2px;
        border-bottom: #EEE solid 1px;
        border-right: #ddd solid 1px;
        border-left: #ddd solid 1px;
      -webkit-transition: all 600ms cubic-bezier(0.165, 0.84, 0.44, 1);
      transition:         all 600ms cubic-bezier(0.165, 0.84, 0.44, 1);
    }       

    .switch-field2 label {
      display: inline-block;
      width: 113px;
      height: 40px;
      background-color: #EFEFEF;
      color: #cdcdcd;
      font: 16px "adiHaus",Arial,sans-serif;
      font-weight: bold;
      text-align: center;
      text-shadow: none;
      padding: 10px 14px;
      border: 1px solid rgba(0, 0, 0, 0.2);
     border-top: #CCC solid 1px;
     border-radius: 2px;
        border-bottom: #EEE solid 1px;
        border-right: #ddd solid 1px;
        border-left: #ddd solid 1px;
     -webkit-transition: all 600ms cubic-bezier(0.165, 0.84, 0.44, 1);
      transition:         all 600ms cubic-bezier(0.165, 0.84, 0.44, 1);
    }       

    .switch-field label:hover, .switch-field2 label:hover {
      cursor: pointer;
    }       

    .switch-field input:checked + label, .switch-field2 input:checked + label {
      background-color: #fff;
      -webkit-box-shadow: none;
      box-shadow: none;
      color:000;
      font-weight: bold;
    }       

    .switch-field label:first-of-type, .switch-field2 label:first-of-type {
    border-right: none;
    }       

    .switch-field label:last-of-type, .switch-field2 label:last-of-type {
    border-left: none;
    }

2 个答案:

答案 0 :(得分:1)

您可以尝试MoreToggles.css,它是一个纯CSS库。

您可以选择一种样式,该样式可以根据需要从右向左滑动。

<head>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/JNKKKK/MoreToggles.css/output/moretoggles.min.css">
</head>
<div class="mt-ios"> 
  <input id="1" type="checkbox" />
  <label for="1"></label>
</div>

答案 1 :(得分:0)

您无法使用两个复选框。嗯,你可能可以,但我个人认为,任何好事都会太乱。所以相反,你应该在你的表中包含一个字段,标题为“Right Handed”,答案是肯定或否定。

所以你创建一个如下的复选框:

我把它放在一张桌子里,以便我可以在任何一侧添加LH和RH。

.tgl {
  display: none;
}
* {
  box-sizing: border-box;
}
.tgl + .tgl-btn {
  outline: 0;
  display: block;
  width: 4em;
  height: 2em;
  position: relative;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  background: #3f9ddd;
  border-radius: 2em;
  padding: 2px;
  -webkit-transition: all .4s ease;
  transition: all .4s ease;
}
.tgl + .tgl-btn:after,
.tgl + .tgl-btn:before {
  position: relative;
  display: block;
  content: "";
  width: 50%;
  height: 100%;
}
.tgl + .tgl-btn:after {
  left: 0;
  border-radius: 50%;
  background: #fff;
  -webkit-transition: all .2s ease;
  transition: all .2s ease;
}
.tgl + .tgl-btn:before {
  display: none;
}
.tgl:checked + .tgl-btn:after {
  left: 50%;
}
.tgl:checked + .tgl-btn {
  background: #2ecc71;
}
<table>
  <tr>
    <td colspan="3">Hand</td>
  </tr>
  <tr>
    <td>LH</td>
    <td>
      <input class="tgl" id="cb1" type="checkbox">
      <label class="tgl-btn" for="cb1"></label>
    </td>
    <td>RH</td>
  </tr>
</table>

因此,如果此人是右撇子,则会选中此复选框。

这不需要JavaScript,CSS基于是否使用:checked前缀选中复选框。