CSS Toggle过渡动画 [英] CSS Toggle transition animation

查看:99
本文介绍了CSS Toggle过渡动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个CSS切换按钮。现在,当您单击该按钮时,将具有淡入和淡出效果。我想表现得更像是从右向左滑动的动画。附带的是HTML,CSS和墨水,用我的代码来摆弄。谢谢! https://jsfiddle.net/ebgpqvha/

I have 2 CSS toggle buttons I am making. Right now when you click the button has a fade in and out effect. I want to to act more like a sliding from right to left animation. Attached is the HTML, CSS and a l ink to a fiddle with my code. Thank you! 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;
    }


推荐答案

您不会能够为此使用两个复选框。好吧,也许可以,但是我个人认为这太混乱了,以至于没有任何好处。因此,相反,您应该在表中包括一个标题为 Right Handed的字段,答案为是或否。

You aren't going to be able to use two check boxes for this. Well you probably could, but I personally think it would be far too messy to be any good. So instead, you should include a field in your table, which is titled something like "Right Handed" and the answer is yes or no.

因此,您创建了一个复选框,例如因此:

So you create a checkbox like so:

我已将其封闭在表格中,以便可以在左右各边添加LH和RH。

I've enclosed it in a table so that I can add the LH and RH either side.

.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 前缀。

This requires no JavaScript, the CSS is based on whether the checkbox is checked or not using the :checked prefix.

这篇关于CSS Toggle过渡动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆