【问题标题】:How to use Font Awesome for checkboxes etc如何将 Font Awesome 用于复选框等
【发布时间】:2012-06-28 17:34:04
【问题描述】:

我在我的网站中使用Font Awesome 作为图标。我对 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

请指导我!!

【问题讨论】:

  • alt-checkbox.starikovs.com 是您寻找的一个很好的例子。这是一个 jQuery 插件,允许使用图标字体和 CSS 设置复选框的样式。

标签: jquery css twitter-bootstrap font-awesome


【解决方案1】:

这是我的simple CSS-only method

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 source 获取要添加的字符。

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]-->

【讨论】:

  • 不要更改 span:before 中的 font-size 并且字体与被包围的元素大小相同。
  • 字体大小使它比使用原始元素的字体大小更好地排列(如果它是引导程序的默认值。)
  • 如果你的字体大小比你使用的更大或更小,那么排队是最糟糕的。你也可以使用 em i。 e.: 1.2em...
  • 你是个天才,先生!我只是添加了一些 display:inline-block; width:3em; 而不是 padding-right 用于固定宽度
  • 有人向我指出了一个帖子,展示了支持 tab 键的精美复选框 css 解决方案:codepen.io/CreativeJuiz/pen/zqKtp
【解决方案2】:

许多花哨的复选框解决方案都存在删除原始输入复选框的缺陷,因为它不接收输入焦点。
这是不可接受的,原因有两个:

  1. 您不能使用键盘(Tab-keyspacebar)导航到复选框并更改其值。
  2. 您不能在复选框上应用悬停和聚焦样式。

@konsumer 上面的解决方案很好,但缺乏输入焦点能力。 但是我遇到了
implementation @geoffrey_crofte 输入焦点所在的位置。但是它可能不像@konsumers 那样花哨

所以.. 我将这两者结合起来并发布了plunker example。 此解决方案的唯一缺点是您必须使用特定的 html 标记才能使其工作:

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

复选框后面必须跟一个标签标签。然后label 标签可以包括一个span 标签,包括标签文本。

我还要求将fancy-check 类用于要应用的新样式。这是为了避免样式破坏页面中缺少所需的其他复选框,位于label 标记之后。

该示例基于使用图标字体,在这种情况下它需要FontAwesome library

样式表在这里:

/*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;
}

【讨论】:

  • 我刚刚将它合并到一个项目中,效果非常好!我使用了 Fontello 而不是 FontAwesome,但它工作得非常顺利。我还扩展了你的 CSS 来处理单选按钮。
  • 感谢,我在我的项目中也使用了它,我对此非常满意。
  • 我将这个解决方案也用于动态字体大小:plnkr.co/edit/i5aqNtt3VTwTg0u21DCV?p=preview
【解决方案3】:
$("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 部分

【讨论】:

  • 这总是跑到 else 部分。我猜 :checked 属性对我的输入元素不可用。你能看看我哪里出错了。非常感谢!!
  • 更新了我的答案,看到你正在使用 Font Awesome
  • 太棒了!你让我开心:)
【解决方案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;
}

【讨论】:

    【解决方案5】:

    看看这个: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; 
    } 
    

    【讨论】:

      【解决方案6】:

      这是一个非常干净的全 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; }
      

      【讨论】:

      • 你应该解释一下解决方案背后的主要思想。
      猜你喜欢
      • 1970-01-01
      • 2019-12-16
      • 2014-06-11
      • 1970-01-01
      • 2018-09-21
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多