【问题标题】:How to use font Awesome instead of images in jquery rating stars如何在 jquery 评级星中使用字体 Awesome 而不是图像
【发布时间】:2014-02-26 11:11:49
【问题描述】:

我正在使用 jquery.raty.min.js 来显示星级,在 js 方法中我可以选择更改显示开始图像,如

starOff:"/images/star_none.png",

starOn:"/images/star_full.png"

但在这里我想使用字体 Awesome 类,例如

<i class="fa fa-star-o"></i>
<i class="fa fa-star"></i>

我尝试过像下面这样放置类

starOff:"<i class='fa fa-star-o'></i>",

starOn:"<i class='fa fa-star'></i>"

但它不起作用任何人都可以帮助这样做。

【问题讨论】:

  • staffOffstarOn 似乎改变了图像的 src 属性或 CSS background 属性。您不能以这种方式创建图标。
  • 你能建议我怎么做吗?
  • 我不确定您的 jquery 或 javascript 代码是如何工作的。因此,除非您添加更多代码,否则我无法为您提供更多帮助。
  • 你能发布整个代码吗?也许有一种方法可以创建 &lt;i&gt; 而不是 &lt;img&gt;
  • 我没有根据这个文档自定义我使用的任何代码wbotelhos.com/raty

标签: javascript jquery css raty


【解决方案1】:

对此问题的简短回答是使用Raty (2.7.0) 的最新更新(截至此回复的最后一次编辑)并查看“starType”属性。当您设置元素时,您将执行以下操作:

$('div').raty({
  // This is the config you need to change the tag from image to icon
  starType : 'i'
});

这里有更多细节...如果您查看 CSS 文件和 fonts 文件,您会看到它们如何设置 &lt;i&gt; 标记以使用特定的字体。从那里它是一个简单的任务来确定每个类的:before 内容将是什么。首先确保包含 FontAwesome,然后像这样配置你的 CSS(特定于 FontAwesome):

.cancel-on-png, .cancel-off-png, .star-on-png, .star-off-png, .star-half-png {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;

  /*This is the important part*/
  font-family: "FontAwesome";

  font-style: normal;
  font-variant: normal;
  font-weight: normal;
  line-height: 1;
  speak: none;
  text-transform: none;
}

.cancel-on-png:before {
  /*the "\f057" represents an unfilled circle with an x in it*/
  /*each of these can be configured to be whichever icon you would like*/
  /*fore more information on the icon code, look at the link listed below*/
  content: "\f057";
}

.cancel-off-png:before {
  content: "\f05c";
}

.star-on-png:before {
  content: "\f005";
}

.star-off-png:before {
  content: "\f006";
}

.star-half-png:before {
  content: "\f123";
}

FontAwesome 的 CSS 内容值的完整列表可以在 here 找到


最终产品看起来像这样:

<div>
  <i title="Cancel this rating!" class="raty-cancel cancel-off-png" data-alt="x"></i>&nbsp;
  <i data-alt="1" class="star-off-png" title="Bad"></i>&nbsp;
  <i data-alt="2" class="star-off-png" title="Below Average"></i>&nbsp;
  <i data-alt="3" class="star-off-png" title="Average"></i>&nbsp;
  <i data-alt="4" class="star-off-png" title="Above Average"></i>&nbsp;
  <i data-alt="5" class="star-off-png" title="Great"></i>
  <input name="score" type="hidden">
</div>

当这样配置时:

$(div).raty({
  readOnly    : readOnly,
  cancel      : !readOnly,
  noRatedMsg  : 'This component has not been rated yet',
  half        : true,
  starType    : 'i',
  hints       : ['Bad', 'Below Average', 'Average', 'Above Average', 'Great'],
  click       : function(score, event) {
    console.log("The score is:", score);
  }
});

我知道已经 3 个月了,但我希望这对某人有所帮助!

【讨论】:

  • 谢谢乔纳森。你节省了我很多时间。
  • @RJD22 感谢您留下评论!很高兴知道我提供了帮助:)
【解决方案2】:

我不认为有一个简单的方法而不修改插件本身。如果您查看jquery.raty.js 文件,请搜索:

$('<img />', { src : icon, alt: i, title: title }).appendTo(this);

这就是创建图像元素,

您需要做的是创建图标。我认为将其更改为:

$('<i />', { class : icon  }).appendTo(this);

但用法是:

starOff:"fa fa-star-o",

starOn:"fa fa-star"

我还没有测试过,但这应该是一个开始,你可能需要使用 CSS 调整图标外观

【讨论】:

  • 有什么错误吗?你能看到至少在 Dom 中创建的&lt;i&gt; 吗?
  • 您好,它的工作方式是我遵循您的建议的方式,现在显示很好,但是在给出评级时,我无法在鼠标悬停之前给出它曾经用来更改图像和点击它曾经给我分数,但现在它不工作。
  • 也许你应该只使用图像...如果你担心视网膜设备的质量,只需将其上传 2 倍大小
  • 如果您查看有关 Raty 最新版本的文档,他们已经添加了功能来弥补此功能。您不再需要更改源代码。我已经在下面发布了解决方案,并附有如何实施它的示例。
【解决方案3】:

也许我无法抓住现有答案的重点,但我发现在 raty 2.7.1 中使用 fontawesome 图标并不难。 这是我的代码。

html:

<div class="score-star" id="star-appearance" rel="score-appearance">
</div>

css:

.score-star {
    color: #07b062;
    font-size: 1.5em;
}

js:

$('.score-star').each(function () {
    $(this).raty({
        number: 5,
        starType: 'i',
        starOff: 'fa fa-star-o',
        starOn: 'fa fa-star'
    });
});

率:

<link href="//cdn.bootcss.com/raty/2.7.1/jquery.raty.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/raty/2.7.1/jquery.raty.min.js"></script>

字体真棒:

<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">

希望对您有所帮助。

【讨论】:

    【解决方案4】:

    请检查字体大小和颜色。添加属性字体大小和颜色。

    【讨论】:

      【解决方案5】:

      使用 font awesome 5,代码应如下所示:

      .star-on-png, .star-off-png, .star-half-png {
        -moz-osx-font-smoothing: grayscale;
        -webkit-font-smoothing: antialiased;
      
        /*This is the important part*/
        font-family: 'Font Awesome 5 Free';
      
        font-style: normal;
        font-variant: normal;
        font-weight: normal;
        line-height: 1;
        speak: none;
        text-transform: none;
      }
      
      .star-on-png:before {
        content: "\f005";
        font-weight: 900;
      }
      
      .star-off-png:before {
        content: "\f005";
      }
      
      .star-half-png:before {
        content: "\f5c0";
        font-weight: 900;
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-23
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多