2016-05-23 19 views
1

私は2つのCSSトグルボタンを作成しています。今すぐボタンをクリックすると、フェードインとアウトの効果があります。私は右から左のアニメーションにスライドするようにもっと働きたい。添付されているのはHTML、CSS、私のコードを使ったフィドルです。ありがとうございました! https://jsfiddle.net/ebgpqvha/CSSトランジションアニメーションを切り替えます

<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

あなたは現在のhtmlでこれを行うことはできません。これは、それらが簡単に分離して動作するためです。スライドするには、1つのチェックボックスにする必要があります。何かこのように:https://jsfiddle.net/CalvT/227dk2ow/ - あなたが望むものがあれば教えてください。私は答えを投稿します –

+0

確かに、どんな助けも大歓迎です。 – sizemattic

+0

例としてbgのスライドエフェクトだけを必要としない限り、@CalvTは正しいです。https://jsfiddle.net/ebgpqvha/1/ – DaniP

答えて

0

このために2つのチェックボックスを使用することはできません。まあ、おそらくできるかもしれませんが、私は個人的にはそれがあまりにも面倒で、どんな良いものでもないと思います。その代わりに、テーブルに "右利き"のような名前のフィールドを含める必要があります。答えは "はい"または "いいえ"です。

だからあなたはそうのようなチェックボックスを作成:私は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を必要とせず、チェックボックスがチェックされているかどうかに基づいており、接頭辞は:checkedです。

関連する問題