【问题标题】:How to fill a percentage of a glyphicon [Bootstrap 3]如何填充字形图标的百分比 [Bootstrap 3]
【发布时间】:2015-11-07 13:35:59
【问题描述】:

您好,我有 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>

【问题讨论】:

    标签: html css twitter-bootstrap glyphicons


    【解决方案1】:

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

    这是我整理的一个代码笔来说明它:http://codepen.io/anon/pen/ogBWwX 下面复制代码 sn-ps 以供参考,其中包含一些超出演示所需的内容。

    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 设置为 display:table-cell 以防止它们环绕并将它们放置得更近。星星之间的空间越大,浮点数的结果就越不准确。

    【讨论】:

    • 我突然想到,如果您在每颗星上使用overflow:hidden div,您可以使这更加灵活和准确。所以,3.4 的评分会给你 5 个 div,其中 3 个设置为 100%、1 到 40% 和 1 到 0% 宽度。
    • 你先生,是个炸弹!谢谢+1
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        虽然这些答案都是正确的(关于多种颜色),而且很长而且可能过于精确(重叠的 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 这种方式。

        【讨论】:

          【解决方案5】:

          Font-awesome 有现成的星系统解决方案: 查看“Star Ratings(受 CSS Tricks 启发)” in their website

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-07
          • 1970-01-01
          • 2013-10-23
          • 1970-01-01
          • 2021-10-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多