使用 jQuery 和 CSS 将数字转换为星级显示
我一直在研究 jquery 插件,想知道如何调整该插件,将数字(如 4.8618164)转换为 4.8618164 颗星,满分 5 颗。基本上将数字 <5 解释为 5 星评级系统中填充的星数,使用jQuery/JS/CSS。
请注意,这只会显示/显示现有数字中的星级,而不接受新的评级提交。
I have been looking at jquery plugin and was wondering how to adapt that plugin to turn a number (like 4.8618164) into a 4.8618164 stars filled out of 5. Basically interpreting a number <5 into stars filled in a 5-star rating system using jQuery/JS/CSS.
Note that this would only display/show the stars rating from an already available number and not accept new ratings submissions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是为您提供的一种解决方案,仅使用一张非常小且简单的图像和一个自动生成的 span 元素:
CSS
Image
注意: 不要热链接到上图!将文件复制到您自己的服务器并从那里使用它。
jQuery
如果您想将星星限制为只有一半或四分之一星星大小,请在
var size
行之前添加以下行之一:HTML
使用
输出
演示
这可能适合您的需求。使用这种方法,您不必计算任何四分之三或诸如此类的星形宽度,只需给它一个浮点数,它就会给您星形。
关于星星如何呈现的一个小解释可能是合适的。
该脚本创建两个块级跨度元素。两个跨度最初的大小均为 80px * 16px,背景图像为 Stars.png。 Span 是嵌套的,因此 Span 的结构如下所示:
外部 Span 的
background-position
为0 -16px
。这使得外部跨度中的灰色星星可见。由于外部跨度的高度为 16px 和repeat-x
,因此它只会显示 5 颗灰色星星。另一方面,内部跨度的
background-position
为0 0
,这使得只有黄色星星可见。这当然适用于两个单独的图像文件:star_yellow.png 和 star_gray.png。但由于星星的高度是固定的,我们可以轻松地将它们组合成一张图像。这利用了 CSS sprite 技术。
现在,当跨度嵌套时,它们会自动相互覆盖。在默认情况下,当两个跨度的宽度均为 80px 时,黄色星星完全遮盖了灰色星星。
但是当我们调整内部跨度的宽度时,黄色星星的宽度减小,露出灰色星星。
在可访问性方面,明智的做法是将浮点数保留在内部范围内,并使用
text-indent: -9999px
隐藏它,这样关闭 CSS 的人至少可以看到浮点数字而不是星星。希望这是有道理的。
更新时间:2010/10/22
现在更加紧凑,更难理解!也可以压缩成一个内衬:
Here's a solution for you, using only one very tiny and simple image and one automatically generated span element:
CSS
Image
Note: do NOT hotlink to the above image! Copy the file to your own server and use it from there.
jQuery
If you want to restrict the stars to only half or quarter star sizes, add one of these rows before the
var size
row:HTML
Usage
Output
Demo
This will probably suit your needs. With this method you don't have to calculate any three quarter or whatnot star widths, just give it a float and it'll give you your stars.
A small explanation on how the stars are presented might be in order.
The script creates two block level span elements. Both of the spans initally get a size of 80px * 16px and a background image stars.png. The spans are nested, so that the structure of the spans looks like this:
The outer span gets a
background-position
of0 -16px
. That makes the gray stars in the outer span visible. As the outer span has height of 16px andrepeat-x
, it will only show 5 gray stars.The inner span on the other hand has a
background-position
of0 0
which makes only the yellow stars visible.This would of course work with two separate imagefiles, star_yellow.png and star_gray.png. But as the stars have a fixed height, we can easily combine them into one image. This utilizes the CSS sprite technique.
Now, as the spans are nested, they are automatically overlayed over each other. In the default case, when the width of both spans is 80px, the yellow stars completely obscure the grey stars.
But when we adjust the width of the inner span, the width of the yellow stars decreases, revealing the gray stars.
Accessibility-wise, it would have been wiser to leave the float number inside the inner span and hide it with
text-indent: -9999px
, so that people with CSS turned off would at least see the floating point number instead of the stars.Hopefully that made some sense.
Updated 2010/10/22
Now even more compact and harder to understand! Can also be squeezed down to a one liner:
《纪元 2022》中有更多适用于现代浏览器的新方法,如果您可以在正确的位置生成评级/百分比,则需要更少的运行时代码。
让您的框架在正确的
style
位置生成此 html,例如33.333%
,然后您就可以开始了:Anno 2022 there's more new methods available for modern browsers that require less runtime code if you can generate the rating/percentage in the right location.
Have your framework generate this html with e.g.
33.333%
in the rightstyle
spots and you're good to go:The original answer that accrued the first 40 upvotes then too:
If you only have to support modern browsers, you can get away with:
You only need to convert the number to a
class
, e.g.class='stars-score-50'
.First a demo of "rendered" markup:
Then a demo that uses a wee bit of code:
The biggest downsides of this solution are:
width
on a pseudo-element).To fix this the solution above can be easily tweaked. The
:before
and:after
bits need to become actual elements in the DOM (so we need some JS for that).The latter is left as an excercise for the reader.
尝试这个 jquery 辅助函数/文件
jquery.Rating.js
index.html
Try this jquery helper function/file
jquery.Rating.js
index.html
为什么不只拥有五个单独的星星图像(空、四分之一满、半满、四分之三满和满),然后根据评级的截断或绕行值乘以 4 将图像注入到 DOM 中(得到季度的整数)?
例如,4.8618164 乘以 4 并四舍五入为 19,即四又四分之三星。
或者(如果您像我一样懒),只需从 21 个图像(0 星到 5 星,以四分之一增量)中选择一个图像,然后根据上述值选择单个图像。然后,只需进行一次计算,然后在 DOM 中更改图像(而不是尝试更改五个不同的图像)。
Why not just have five separate images of a star (empty, quarter-full, half-full, three-quarter-full and full) then just inject the images into your DOM depending on the truncated or rouded value of rating multiplied by 4 (to get a whole numner for the quarters)?
For example, 4.8618164 multiplied by 4 and rounded is 19 which would be four and three quarter stars.
Alternatively (if you're lazy like me), just have one image selected from 21 (0 stars through 5 stars in one-quarter increments) and select the single image based on the aforementioned value. Then it's just one calculation followed by an image change in the DOM (rather than trying to change five different images).
我最终完全不用 JS 来避免客户端渲染延迟。为了实现这一点,我生成如下 HTML:
为了帮助实现可访问性,我什至在标题属性中添加原始评级值。
I ended up going totally JS-free to avoid client-side render lag. To accomplish that, I generate HTML like this:
To help with accessibility, I even add the raw rating value in the title attribute.
DEMO
您只需使用 2 张图片即可完成此操作。 1 个空白星星,1 个实心星星。
将填充的图像叠加在另一幅图像的顶部。并将评级数转换为百分比并将其用作填充图像的宽度。
DEMO
You can do it with 2 images only. 1 blank stars, 1 filled stars.
Overlay filled image on the top of the other one. and convert rating number into percentage and use it as width of fillter image.
使用 CSS
为了实现可访问性,您还可以添加 Aria 属性,例如
aria-label="Rating: 4.5 star"
。使用 CSS 和 JavaScript 进行星级评级
虽然上面的示例非常适合评级预览目的,但如果您想允许用户投票,则需要添加一些 JavaScript 来处理点击、UX 颜色指针交互的变化等 - 请参阅此相关答案:
带有指针事件的星级
Stars rating using CSS
For accessibility you can also add the Aria attribute like
aria-label="Rating: 4.5 stars"
.Stars rating using CSS and JavaScript
Whilst the above example is great for rating preview purpose, if you want to allow the user to cast a vote, you'll need to add some JavaScript for handling click, UX colors change on pointer interaction, etc. — See this related answer:
Star rating with pointer events
使用没有原型的jquery,将js代码更新为
我还在span中添加了一个名为data- rating的数据属性。
using jquery without prototype, update the js code to
I also added a data attribute by the name of data-rating in the span.
这是我使用 JSX 和很棒的字体的看法,不过精度仅限于 0.5:
第一行是全星,第二行是半星(如果有的话)
Here's my take using JSX and font awesome, limited on only .5 accuracy, though:
First row is for whole star and second row is for half star (if any)