如何填写glyphicon的百分比[Bootstrap 3]

时间:2014-01-30 21:54:33

标签: html css twitter-bootstrap glyphicons

您好我有5星评级系统,我想这样做,所以我可以添加说4.3星而不是4.我怎么会这样做?我目前的代码如下:

<center>
  <span class="glyphicon glyphicon-star" style="color:#BBD41C"></span>
  <span class="glyphicon glyphicon-star" style="color:#BBD41C"></span>
  <span class="glyphicon glyphicon-star" style="color:#BBD41C"></span>
  <span class="glyphicon glyphicon-star" style="color:#BBD41C"></span>
  <span class="glyphicon glyphicon-star"></span>
</center>

5 个答案:

答案 0 :(得分:6)

你可以重叠2个绝对定位的div。每个都包含所有5颗星。然后顶部div可能有一个溢出:隐藏的div,其中您设置宽度等于百分比(基于评级)。

以下是我编写的一个代码集,用于说明它:http://codepen.io/anon/pen/ogBWwX代码片段在下面复制以供参考,并且为了演示的目的,包含了一些额外的代码片段。

<强> HTML:

<div class="container">
  <div class="flt_left">
    <form id="star_form">
      <label for="star_input">Stars:</label>
      <input type="text" id="star_input" />
      <button id="setStars">Set Rating</button>
    </form>
  </div>
  <div class="flt_left">
    <div id="star_box">
      <div class="star_limiter">
        <div class="longbar">
          <span class="glyphicon glyphicon-star"></span>
          <span class="glyphicon glyphicon-star"></span>
          <span class="glyphicon glyphicon-star"></span>
          <span class="glyphicon glyphicon-star"></span>
          <span class="glyphicon glyphicon-star"></span>
        </div>
      </div>
    </div>
    <div class="star_bg">
      <span class="glyphicon glyphicon-star"></span>
      <span class="glyphicon glyphicon-star"></span>
      <span class="glyphicon glyphicon-star"></span>
      <span class="glyphicon glyphicon-star"></span>
      <span class="glyphicon glyphicon-star"></span>
    </div>
  </div>
  <div style="clear:both;"></div>
  <p id="rating_data"></p>
</div>

<强> CSS:

.container{
  padding: 15px;
  margin: 10px;
}
p{
  margin: 10px 0;
}
.flt_left{
  float: left;
  margin-right: 10px;
  position: relative;
  /*min-width: 250px;*/
}
#star_box,
.star_bg{
  color: #BBD41C;
  position: absolute;
  margin: 4px 0;
  top: 0;
  left: 0;
  display: table;
}
.glyphicon{
  display: table-cell;
  padding: 0 0 0 2px;
  font-size: 18px;
}
#star_box{
  z-index: 2;
}
.star_bg{
  color: #555;
}
#star_box .star_limiter{
  width:100%;
  overflow: hidden;
  position: relative;
}

<强>使用Javascript:

function setWidth(perc){
  $('#star_box .star_limiter').css('width',perc+'%');
}
function starToPercent(amt,outof){
  if(typeof outof == 'undefined') outof = 5;
  var percent = Math.round(amt*100/outof);
  if(percent > 100) percent = 100;
  $('#rating_data').text(amt+' of '+outof+' stars, or '+percent+'%');
  setWidth(percent);
}
function setRating(){
  var input = parseFloat($('#star_input').val());
  var num_stars = $('#star_box .star_limiter .glyphicon-star').length;


  if(!isNaN(input)) starToPercent(input, num_stars);
}

$(document).ready(function(){
  var star_width = $('#star_box').width();
  var star_height = $('#star_box').height();
  $('#star_box').css('width',star_width+'px');
  $('#star_box .star_limiter').css('height',star_height+'px');
  $('#star_box .longbar').css('width',star_width+'px');
  
  $('#setStars').click(setRating);
  $('#star_form').submit(function(e){
    e.preventDefault();
    setRating();
  });
});

备注: 重叠的星星看起来不太好,因为你会看到边缘周围的背景星的颜色。因此,如果你摆脱背景恒星并使用纯色,它看起来会更好。与颜色匹配越多,它也越不明显。

我设置了glyphicons的CSS来显示:table-cell,以防止它们包裹并将它们放在一起。恒星之间的空间越大,浮标就越不准确。

答案 1 :(得分:4)

您不能在图标字体上使用多种颜色,因此您无法提出要求。您可以使用部分背景填充,但这似乎不太理想。

如果您切换到Font Awesome,您至少可以访问half-star个符号。

答案 2 :(得分:2)

我发现这有效,主要缺点是您需要根据字体大小设置特定类的样式:

HTML:

 <i class="glyphicon glyphicon-star"></i>
 <i class="glyphicon glyphicon-star"></i>
 <i class="glyphicon glyphicon-star"></i>
 <i class="glyphicon glyphicon-star"></i>
 <i class="glyphicon glyphicon-star half-star"></i>

CSS:

.half-star {
        width:5px; // Roughly half the font size
        overflow:hidden;
 }

答案 3 :(得分:1)

虽然这些答案都是正确的(关于多种颜色)和长而且可能太精确(重叠的div,它使用像素来移动东西),但我发现了一个很好的黑客,现在涉及:after选择器。

基本上,如果您知道背景颜色是什么,您可以通过这种方式覆盖角色的一半(或任何需要的百分比):

HTML:

     <span class="glyphicon glyphicon-star"></span>
     <span class="glyphicon glyphicon-star"></span>
     <span class="glyphicon glyphicon-star"></span>
     <span class="glyphicon glyphicon-star"></span>
     <span class="glyphicon glyphicon-star half-star"></span>`

CSS:

   .half-star {
     letter-spacing: -0.5em;
       &:after {
       position: absolute;
       content: "";
       left: 0.5em;
       width: 0.5em;
       height: 1em;
       background-color: $body-bg;
     }
   }

首先,我将字母间距设置为字体默认值的一半,这样可以正确对齐以下文本,星形或其他元素。然后,我创建了一个绝对定位的$body-bg块的宽度为恒星的一半并且在恒星的中间(这是4.5星级)。您可以更改该小数以匹配您想要的评级。

你不能改变字形的颜色,并且在第一个上添加不同的颜色或字形需要更多的黑客攻击(想象的方式与我展示的相同但是从另一个方向)但是它可以在没有任何颜色的情况下完成Javascript,纯粹用HTML和CSS这种方式。

答案 4 :(得分:1)

Font-awesome有一个现成的星系统解决方案: 看看“星级评分(灵感来自CSS技巧)” in their website