コピペで簡単!CSSとjQueryでラジオボタンを装飾する
デフォルトのラジオボタンは見栄えが…
ってときには装飾してしまえばいいのだ
ってことでCSSとjQueryで装飾してみる。
HTML
- <label class="fed-radio fed-flxMid selected">
- <i class="fed-icRadio"></i>
- <span>テストラジオ①</span>
- <input type="radio" name="test" class="fed-inputRadio fed-hide" value="0" checked="true">
- </label>
- <label class="fed-radio fed-flxMid">
- <i class="fed-icRadio"></i>
- <span>テストラジオ②</span>
- <input type="radio" name="test" class="fed-inputRadio fed-hide" value="1">
- </label>
CSS
- .fed-flxMid {
- display: -webkit-flex;
- display: flex;
- -webkit-flex-wrap: wrap;
- flex-wrap: wrap;
- -webkit-align-items: center;
- align-items: center;
- }
- .fed-icRadio {
- position: relative;
- width: 20px;
- height: 20px;
- margin-right: 0.5em;
- background-color: #fff;
- border-radius: 100%;
- border: 1px solid #ccc;
- box-sizing: border-box;
- }
- .fed-icRadio::before {
- content: '';
- position: absolute;
- top: 4px;
- left: 4px;
- width: 10px;
- height: 10px;
- border-radius: 100%;
- background-color: #ccc;
- }
- .selected .fed-icRadio {
- border-color: #2e9afe;
- }
- .selected > .fed-icRadio::before {
- background-color: #2e9afe;
- }
- .fed-hide {
- display: none;
- }
jQuery
- <script>
- // ラジオボタンON/OFF inputの状態に順ずる
- function radioBtnChange(s){
- $('body').on('change',s,function(e){
- $('[name="'+e.currentTarget.name+'"]').each(function(i,k){
- if(k.checked){
- $(k).closest('label').addClass('selected');
- }else{
- $(k).closest('label').removeClass('selected');
- }
- });
- });
- }
- $(function(){
- // ラジオON/OFF
- radioBtnChange('.fed-inputRadio');
- });
- </script>
まとめ
仕組みはたったこれだけ、CSSを変更すればどんな表示も思いのまま!?
ぜひ使って見てください^^
コメント一覧
まだ、コメントがありません