16

WebサイトのアイコンにFontAwesomeを使用しています。私はHTMLとCSSにかなり慣れていません。これをチェックボックスに使用しようとしていたのですが、チェックボックスを有効/無効にして適切なアイコンを表示する方法を見つけるのに苦労しました。

例えば; チェックボックスに以下のタグを使用しています。

<div style="font-size: 24px;">
<i id="prime" type="checkbox" class="icon-check-empty"></i>icon-check
</div>

チェックボックスが有効/無効になっているときにアイコンを変更するために、以下のjQueryを使用しています。

$(".icon-check-empty").click(function(){
    $('#prime').removeClass('icon-check-empty');
    $('#prime').addClass('icon-check');
});

私はこれがそれがどのように使われるべきかという方法ではないと確信しています。使い方を教えてください。

今のところ; チェックボックスを使用したいのですが、チェックボックスをオン/オフにして、対応するアイコンを表示することを目標としています。チェックした場合:icon-check; チェックされていない:icon-check-empty

案内してください!!

4

6 に答える 6

47

これが私の単純なCSSのみの方法です:

input[type="radio"],
input[type="checkbox"] {
  display:none;
}

input[type="radio"] + span:before,
input[type="checkbox"] + span:before {
  font-family: 'FontAwesome';
  padding-right: 3px;
  font-size: 20px;
}

input[type="radio"] + span:before {
  content: "\f10c"; /* circle-blank */
}

input[type="radio"]:checked + span:before {
  content: "\f111"; /* circle */
}

input[type="checkbox"] + span:before {
  content: "\f096"; /* check-empty */
}

input[type="checkbox"]:checked + span:before {
  content: "\f046"; /* check */
}

基本的な考え方は、スパンを選択することです。その前に、必要な入力の横にあります。チェックボックスとラジオを使って例を作りました。less / sassを使用している場合は、.icon-glass:before宣言を含めるだけで、すべての保守と変更が簡単になります。

補足として、この方法を使用して好きなようにスタイルを設定できるので、色、背景、影の色、アイコンなどを変更します。

FontAwesomeソースを使用して、追加するキャラクターを取得できます。

selectivizrは:before&:checkedをサポートしているため、IE6-8をサポートしている場合は、それを使用してIE6+をサポートします。

<!--[if (gte IE 6)&(lte IE 8)]>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/selectivizr/1.0.2/selectivizr-min.js"</script>
<noscript><link rel="stylesheet" href="[fallback css that display:inline-block radios & checkboxes]" /></noscript>
<![endif]-->
于 2013-01-09T00:39:12.873 に答える
11

そこにある派手なチェックボックスソリューションの多くには、入力フォーカスを受け取らないように元の入力チェックボックスを削除するという欠陥があります。
これは、次の2つの理由で受け入れられません。

  1. Tab-keyキーボード(およびspacebar)を使用してチェックボックスに移動し、その値を変更することはできません。
  2. チェックボックスにホバースタイルとフォーカススタイルを適用することはできません。

@konsumerによる上記の解決策は素晴らしいですが、入力フォーカス機能が不足しています。しかし 、入力フォーカスが機能する@geoffrey_crofteによる
実装に出くわしました。しかし、それはおそらく@konsumersほど派手ではありません

だから..私はそれらの2つを組み合わせて、プランカーの例を出しました。このソリューションの唯一の欠点は、これを機能させるために特定のhtmlマークアップを使用する必要があることです。

<input type="checkbox" class="fancy-check" id="myId"/>
<label for="myId" ><span>The label text</span></label>

チェックボックスの後にはラベルタグが続く必要があります。タグには、ラベルテキストを含むタグをlabel含めることができます。span

fancy-checkまた、適用する新しいスタイルにクラスを使用することを要件にしました。labelこれは、必要な次のタグが欠落しているページ内の他のチェックボックスをスタイルが破壊しないようにするためです。

この例は、アイコンフォントの使用に基づいています。この場合、FontAwesomeライブラリが必要です。

スタイルシートはここにあります:

/*Move he original checkbox out of the way */
[type="checkbox"].fancy-check {
  position: absolute;
  left: -9999px;
}
/*Align the icon and the label text to same height using tabl-cell display*/
/*If you change the font-size of the text, you may also want to do som padding or alignhment changes here*/
.fancy-check ~ label >  span {
  display: table-cell;
  vertical-align: middle;
  padding-left: 5px;
}
/*The label will contain the icon and the text, will grab the focus*/
[type="checkbox"].fancy-check + label {
  cursor: pointer;
  display: table;
}
/*The icon container, set it to fixed size and font size, the padding is to align the border*/
/*If you change the font-size of this icon, be sure to adjust the min-width as well*/
[type="checkbox"].fancy-check + label:before {
  font-family: 'FontAwesome';
  display: inline-block;
  box-sizing: border-box;
  border: 1px solid transparent;
  font-size: 22px;
  min-width: 28px;
  padding: 2px 0 0 3px;
}
/* toggle font awsome icon*/
[type="checkbox"].fancy-check:checked + label:before {
  content: "\f046";
}
[type="checkbox"].fancy-check:not(:checked) + label:before {
  content: "\f096";    
}
/*Do something on focus, in this case show dashed border*/
[type="checkbox"].fancy-check:focus + label:before {
  border: 1px dashed #777;
}
/*Do something on hover, in this case change the image color*/
[type="checkbox"].fancy-check:hover + label:before {
   color: #67afe5;
}
于 2014-08-30T00:18:55.990 に答える
8
$("yourselector").click(function(){
    if($(this).hasClass('icon-check')){
        $(this).removeClass('icon-check').addClass('icon-check-empty');}
    else {
        $(this).removeClass('icon-check-empty').addClass('icon-check');}
});​

yourselector必要に応じてパーツを変更します

于 2012-06-27T10:16:12.230 に答える
4

jQueryを実行している場合は、次のようなものを使用できます。

jQueryを拡張します:

jQuery.fn.extend({
  shinyCheckbox:
    function() {
      var setIcon = function($el) {
        var checkbox = $el.find('input[type=checkbox]');
        var iclass = '';
        if (checkbox.is(':checked')) {
          var iclass = 'icon-check';
        } else {
          var iclass = 'icon-check-empty';
        }
        $el.find('i[class^=icon-]').removeClass('icon-check').removeClass('icon-check-empty').addClass(iclass);
      }
      this.find('input[type=checkbox]').change(function() {
        setIcon($(this).parents('label.checkbox'));
      });
      this.each(function(i, el) {
        setIcon($(el));
      });
    }  
});

$(document).ready(function() {
  $('label.checkbox').shinyCheckbox();
});

そして、これをHTMLで使用します。

<label class="checkbox" for="mycheckbox">
  <i class="icon-check-empty"></i>
  <input type="hidden" name="mycheckbox" value="0" />
  <input id="mycheckbox" name="mycheckbox" type="checkbox" value="1" checked="checked" />
  Meine Checkbox hat einen sehr langen Text
</label>

そして、いくつかの基本的なcssとあなたは光沢のあるチェックボックスを持っています:

input[type="checkbox"] {
    display: none;
}
label.checkbox {
    cursor: pointer;
    display: inline-block;
    margin-left: 1px;
    padding-left: 15px; /* maybe this is not enough for your font size - remember: it's just an example and not best practice */
}
label.checkbox .icon-check:before, label.checkbox .icon-check-empty:before {
    margin-left: -15px;
    padding-right: 3px;
}
于 2012-11-14T22:26:48.520 に答える
1

これを見てください:http://codepen.io/jamesbarnett/pen/yILjk

上記のリンクから、これが私のために働いたコードです。

/*** custom checkboxes ***/

input[type=checkbox] { 
    display:none; 
} 

/* to hide the checkbox itself */
input[type=checkbox] + label:before {
  font-family: FontAwesome;
  display: inline-block;
}

input[type=checkbox] + label:before {
    content: "\f096"; 
} 

/* unchecked icon */
input[type=checkbox] + label:before {    
    letter-spacing: 10px; 
} 

/* space between checkbox and label */
input[type=checkbox]:checked + label:before {
    content: "\f046"; 
} 

/* checked icon */
input[type=checkbox]:checked + label:before { 
    letter-spacing: 5px; 
} 
于 2014-11-16T14:23:24.680 に答える
0

これは非常にクリーンなすべてのCSSの例です。

http://jsfiddle.net/8wLBJ/

.checkbox-container { width: 100%; height: 30px; padding: 0 0 0 10px; margin: 5px 0 0 0; border-radius: 3px; background-color: #e5e5e5; }
.checkbox-container label { float: left; padding: 0; margin-top: 7px; }
.checkbox-container label .checkBoxTitle {width: 200px; margin-top: -1px; font-size: 12px;}

.newCheck[type="checkbox"]:not(:checked), .newCheck[type="checkbox"]:checked { position: absolute; left: -9999px;}
.newCheck[type="checkbox"]:not(:checked) + label, .newCheck[type="checkbox"]:checked + label { width: 150px; position: absolute; cursor: pointer; }
.newCheck[type="checkbox"]:not(:checked) + label:before,   .newCheck[type="checkbox"]:checked + label:before { content: ''; position: absolute; left: 0; top: -1px; width: 17px; height: 17px; border: 1px solid #aaa; background: #f8f8f8; border-radius: 3px; box-shadow: inset 0 1px 3px rgba(0,0,0,.1);}
.newCheck[type="checkbox"]:not(:checked) + label:after,    .newCheck[type="checkbox"]:checked + label:after { content: '\f00c'; font-family: FontAwesome; padding-right: 5px; position: absolute; top: -8px; left: 0;  font-size: 20px; color: #555;}
.newCheck[type="checkbox"]:not(:checked) + label:after { opacity: 0;}
.newCheck[type="checkbox"]:checked + label:after { opacity: 1;}
.newCheck[type="checkbox"]:checked + label span, .newCheck[type="checkbox"]:not(:checked) + label span { position:absolute; left:25px; }
于 2014-04-25T23:37:21.533 に答える