【问题标题】:How do I add a Font Awesome icon to file input field如何将 Font Awesome 图标添加到文件输入字段
【发布时间】:2016-07-27 00:13:41
【问题描述】:

我有一个文件输入按钮,而不是浏览,我想在那里显示 Font Awesome 上传图标,但无论我尝试什么都没有给我结果。

这是我尝试过的:

.select-wrapper {
  background: url(http://s10.postimg.org/4rc0fv8jt/camera.png) no-repeat;
  background-size: cover;
  display: block;
  position: relative;
  width: 33px;
  height: 26px;
}
#image_src {
  width: 26px;
  height: 26px;
  margin-right: 100px;
  opacity: 0;
  filter: alpha(opacity=0); /* IE 5-7 */
}
<div class="container">
  <span class="select-wrapper">
    <input type="file" name="image_src" id="image_src" />
  </span>
</div>

通过上面的代码,我只得到一个图片而不是浏览按钮,但我想使用 Font Awesome 图标。

【问题讨论】:

  • @JacobGray 我尝试在输入类型类中添加图标类
  • @Pangloss 我试过了,但没有结果
  • 确保将其添加到容器而不是输入字段本身。
  • 在这种情况下; span.select-wrapper:before { font-family:'FontAwesome'; content: '\icondata'; }

标签: twitter-bootstrap css font-awesome


【解决方案1】:

您可以使用display: none 隐藏文件input 并创建自定义按钮,或者在这种情况下创建将触发输入的字体真棒图标。您也可以使用span 显示输入值,该值会随着输入值的变化而变化。

$("i").click(function () {
  $("input[type='file']").trigger('click');
});

$('input[type="file"]').on('change', function() {
  var val = $(this).val();
  $(this).siblings('span').text(val);
})
.element {
  display: inline-flex;
  align-items: center;
}
i.fa-camera {
  margin: 10px;
  cursor: pointer;
  font-size: 30px;
}
i:hover {
  opacity: 0.6;
}
input {
  display: none;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">
  <i class="fa fa-camera"></i><span class="name">No file selected</span>
  <input type="file" name="" id="">
</div>

【讨论】:

    【解决方案2】:

    由于您使用的是 Bootstrap - 如果您愿意使用一个小插件,那么有一个可以满足您的需求。插件为 Bootstrap Filestyle - http://markusslima.github.io/bootstrap-filestyle/

    您所要做的就是在您的input 中添加一些属性,然后在 Bootstrap 的 JS 之后下载/包含插件的 JS。

    <input type="file" class="filestyle" name="image_src" id="image_src" data-input="false" data-iconName="fa fa-upload" data-buttonText="Upload File" />
    

    你的字体真棒图标类将进入data-iconName,你的措辞将进入data-buttonText

    此外,您可以使用data-input 添加或删除文本输入部分。真显示它,假(如下面的演示)隐藏它。

    Working Demo Here

    【讨论】:

    • 效果很好 - 但请注意,这需要文件样式的版本 >= 1.2
    【解决方案3】:

    还有另一种方法可以使用 CSS 和 FontAwesome 来处理这种情况。如果您访问 FontAwesome 的网站并查看示例,我将在此示例中使用“fa fa-asterisk”,您会注意到,一旦单击图标,它会将您带到页面,它会给您一个 UNICODE fontawesome 图标的值。例如,星号的 UNICODE 值为“f069”。

    在这种情况下,您可以像这样在 CSS 中使用“之后或之前”伪类:

    [input]#img_src::before {
    font-family: 'FontAwesome';
    content: '\f069'
    }
    

    这将在任何输入按钮文本之前(在这种情况下)放置一个令人敬畏的星号。如果您希望按钮仅显示图标,则只需不要输入任何输入文本,只需使用伪类来处理仅使用 CSS 的 fontawesome 图标的分布。

    希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      .select-wrapper {
        background: url(http://s10.postimg.org/4rc0fv8jt/camera.png) no-repeat;
        background-size: cover;
        display: block;
        position: relative;
        width: 33px;
        height: 26px;
      }
      #image_src {
        width: 26px;
        height: 26px;
        margin-right: 100px;
        opacity: 0;
        filter: alpha(opacity=0); /* IE 5-7 */
      }
      <div class="container">
        <span class="select-wrapper">
          <input type="file" name="image_src" id="image_src" />
        </span>
      </div>

      .select-wrapper {
        background: url(http://s10.postimg.org/4rc0fv8jt/camera.png) no-repeat;
        background-size: cover;
        display: block;
        position: relative;
        width: 33px;
        height: 26px;
      }
      #image_src {
        width: 26px;
        height: 26px;
        margin-right: 100px;
        opacity: 0;
        filter: alpha(opacity=0); /* IE 5-7 */
      }
      <div class="container">
        <span class="select-wrapper">
          <input type="file" name="image_src" id="image_src" />
        </span>
      </div>

      【讨论】:

      • 谁能告诉如何使用 js 添加更多自定义选择文件图像按钮?
      猜你喜欢
      • 2013-04-05
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多