我正在尝试在jQuery Mobile中使用Font Awesome按钮。为此,我按照此帖子中描述的答案进行了操作。然而,当我尝试在按钮中使用图标时,类ui-icon-fa
具有display:inline-block
属性,导致按钮不再是全宽度。我该如何解决这个问题?
我正在尝试在jQuery Mobile中使用Font Awesome按钮。为此,我按照此帖子中描述的答案进行了操作。然而,当我尝试在按钮中使用图标时,类ui-icon-fa
具有display:inline-block
属性,导致按钮不再是全宽度。我该如何解决这个问题?
与参考帖子中创建一堆类不同,我会使用标准的<i class="fa fa-camera-retro"></i>
并通过一些CSS将其正确放置:
<button class="ui-btn ui-btn-fa"><i class="fa fa-camera-retro"></i>hello</button>
.ui-btn-fa {
padding-left: 2.5em;
}
.ui-btn-fa .fa {
position: absolute;
left: 9px;
width: 22px;
height: 22px;
}
.ui-btn-fa .fa:before {
line-height: 22px !important;
}
如果你喜欢jQM标准图标中的灰色圆盘,可以添加一个新的类(例如ui-fa-disk),并添加以下CSS:
<button class="ui-btn ui-btn-fa ui-fa-disk"><i class="fa fa-user"></i>hello</button>
.ui-fa-disk .fa {
background-color: rgba(0, 0, 0, 0.298039);
border-radius: 1em;
font-size: 14px;
}
ui-btn-icon-right
),则此方法中的图标将不会像本机jQuery Mobile图标一样运作。 - Elliot B.如果您需要将图标左对齐、右对齐或应用任何其他jQuery Mobile排列(例如,类ui-btn-icon-right
),则接受的答案将无法正常工作。
我建议使用由dotcastle创建并在此Github repo中维护的库。这些图标的行为与它们是标准jQuery Mobile图标时完全相同。
示例用法:
<span class="ui-btn-icon-notext ui-icon-fa-500px"></span>
data: "Update",
"render": function (data, type, row, meta) {
return '<a href="#container" style="color:white" onclick="changeRM1(this)"><i class="fa fa-pencil-square-o" aria-hidden="true" style="height:50px;width:70px" id="button_' + i + 1 + '" type="button" ></i></a>'
display: block
即可。对于仅包含图标的按钮和带有白色图标的默认样式,我使用以下CSS。
示例
其中一个具有非常宽的图标和圆形图标。演示
<button class="fa fa-address-card ui-btn ui-btn-icon-notext ui-corner-all"/>
<button class="fa fa-plus-circle ui-btn ui-btn-icon-notext ui-corner-all"/>
CSS
.ui-btn.fa.ui-btn-icon-notext
{
text-indent: 0px;
color: #fff;
background-color: #acacac;
padding: 0.1em;
font-size: 0.8985em;
height: 1.75em;
width: 1.75em;
/* use inset box-shadow to simulate black background (:after) with opacity, which did blend white fa icons
default outset black shadow*/
box-shadow: inset 0 0 0px 0.21em #f6f6f6, 0 1px 3px rgba(0,0,0,.15);
text-shadow: none;
}
.ui-btn.fa.ui-btn-icon-notext:focus
{
box-shadow: inset 0 0 0px 0.21em #f6f6f6, 0 0 12px #38c; /* inset and default outset focus shadow */
}
.ui-btn.fa.ui-btn-icon-notext:hover
{
box-shadow: inset 0 0 0px 0.21em #ededed, 0 1px 3px rgba(0,0,0,.15);; /* simulate default hover with box shadow, default outset black shadow */
}
.ui-btn.fa.ui-btn-icon-notext:after
{
background-color: transparent;
content: "";
}
背景:
最初的方法非常简单,但只允许使用黑色或深色的font awesome图标,因为jquery使用以下规则来渲染图标。白色图标将与此after
透明背景混合。我改用inset box-shadow来避免这种情况。
.ui-btn-icon-notext:after
{
background-color:#666;
background-color:rgba(0,0,0,.3);
}