用CSS写的星级评分

2016-02-19 20:23 8 1 收藏

下面,图老师小编带您去了解一下用CSS写的星级评分,生活就是不断的发现新事物,get新技能~

【 tulaoshi.com - Web开发 】

利用层的定位、层的顺序以及背景定位来实现的星级评分。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

HTML部分:

h1CSS: Star Rating Redux/h1
ul class="star-rating"
li class="current-rating" style="width:60%;"Currently 3/5 Stars./li
lia href="#" title="1 star out of 5" class="one-star"1/a/li
lia href="#" title="2 stars out of 5" class="two-stars"2/a/li
lia href="#" title="3 stars out of 5" class="three-stars"3/a/li
lia href="#" title="4 stars out of 5" class="four-stars"4/a/li
lia href="#" title="5 stars out of 5" class="five-stars"5/a/li
/ul

h1CSS: Star Rating Redux inline/h1
Rate this:
span class="inline-rating"
ul class="star-rating small-star"
li class="current-rating" style="width:30%;"Currently 1.5/5 Stars./li
lia href="#" title="1 star out of 5" class="one-star"1/a/li
lia href="#" title="2 stars out of 5" class="two-stars"2/a/li
lia href="#" title="3 stars out of 5" class="three-stars"3/a/li
lia href="#" title="4 stars out of 5" class="four-stars"4/a/li
lia href="#" title="5 stars out of 5" class="five-stars"5/a/li
/ul/span
And This:
span class="inline-rating"
ul class="star-rating small-star"
li class="current-rating" style="width:80%;"Currently 4/5 Stars./li
lia href="#" title="1 star out of 5" class="one-star"1/a/li
lia href="#" title="2 stars out of 5" class="two-stars"2/a/li
lia href="#" title="3 stars out of 5" class="three-stars"3/a/li
lia href="#" title="4 stars out of 5" class="four-stars"4/a/li
lia href="#" title="5 stars out of 5" class="five-stars"5/a/li
/ul
/span
h1Let's make it smaller/h1

ul class="star-rating small-star"
li class="current-rating" style="width:50%"Currently 2.5/5 Stars./li
lia href="#" title="1 star out of 5" class="one-star"1/a/li
lia href="#" title="2 stars out of 5" class="two-stars"2/a/li
lia href="#" title="3 stars out of 5" class="three-stars"3/a/li
lia href="#" title="4 stars out of 5" class="four-stars"4/a/li
lia href="#" title="5 stars out of 5" class="five-stars"5/a/li
/ul
这部分的关键在于current-rating,把它的宽度定为50%,然后设置背景平铺,之后把它的层的顺序设为最低,这样就会形成一个初始的分值。
再就是a标签和hover的灵活应用了:上面的例子中一共有5个a标签,那么每个a标签的宽度就是20%,以此类推给每个a标签都定一个不同的宽度;接着,给每个a标签都定义不同顺序z-index,再把hover的样式用背景定位的方法来实现鼠标悬停后不同的背景切换,正因为我们已经把z-index的顺序都定义好了,所以才会形成不同宽度的a标签的遮盖,就是评分的效果了。

再看下CSS:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

.star-rating,.star-rating a:hover,.star-rating a:active,.star-rating a:focus,.star-rating .current-rating{background:url(star.gif) left -1000px repeat-x;}
.star-rating{position:relative;width:125px;height:25px;overflow:hidden;list-style:none;margin:0;padding:0;background-position:left top;}
.star-rating li{display:inline;}
.star-rating a,.star-rating .current-rating{position:absolute;top:0;left:0;text-indent:-1000em;height:25px;line-height:25px;outline:none;overflow:hidden;border:none;}
.star-rating a:hover,.star-rating a:active,.star-rating a:focus{background-position:left bottom;}
.star-rating a.one-star{width:20%;z-index:6;}
.star-rating a.two-stars{width:40%;z-index:5;}
.star-rating a.three-stars{width:60%;z-index:4;}
.star-rating a.four-stars{width:80%;z-index:3;}
.star-rating a.five-stars{width:100%;z-index:2;}
.star-rating .current-rating{z-index:1;background-position:left center;}

/* for an inline rater */
.inline-rating{display:-moz-inline-block;display:-moz-inline-box;display:inline-block;vertical-align:middle;}

/* smaller star */
.small-star{width:50px;height:10px;}
.small-star,.small-star a:hover,.small-star a:active,.small-star a:focus,.small-star .current-rating{background-image:url(star_small.gif);line-height:10px;height:10px;}
这个效果懂得人理解起来就非常方便,不懂的人是需要费些时间来理解,就像我是费了些时间来理解。

资料引用:http://www.knowsky.com/441347.html

来源:https://www.tulaoshi.com/n/20160219/1623531.html

延伸阅读
标签: Web开发
初步介绍 当然,我知道现在有成千上万个关于 用CSS处理圆角 的教程,但不管怎么说,我仍然想把这篇文章展示给您。也希望您会发现这篇文章会非常有用。需要重点指出的是,这篇教程彻底地应用高级CSS技术,但是,我会尽力使初学者看起来简单。 CSS3 在这里还没有得到完全的应用,所以,知道现在,我会保持W3C验证的有效。 看一下演示 ...
文章摘要 : CGI规定了Web服务器调用其他可执行程序(CGI程 序)的接口协议标准。Web服务器通过调用CGI程序实现和Web浏览器的交互。CGI程序可以用任何程序设计语言编写,如Shell脚本语言、Perl、Fortran、Pascal、C语言等。 !-- frame contents -- !-- /frame contents -- 但是用C语言编写的CGI程序具有执行速度快、安全性高等特...
演讲比赛评分系统可以对参加比赛的选手进行评分,在本例中将8个评委的分数相加得出总分sum并计算出最后得分(平均分),精确到小数点后3位。如评一等奖1名,二等奖2名,三等奖3名。主要的任务就是界面设置、全局变量设置(使用到了VBA,不熟悉的朋友可以参考相关文章)、评奖模块的建立、显示获奖名单的建立等等,在文章末尾有源程序下载,感兴趣...
标签: Web开发
在cssplay网站看到有一组CSS像素画,于是也想摩仿一下,于是在网络上找到一组头像图标,看其结构比较简单,就拿它开刀吧!先看看预览图 图一 基本原理 : 没有什么技术含量,主要是利用背景色和边框色来渲染每一个像素点,加上绝对定位,就可以变换出不同的风格。在制作一幅图片之前最好是将图片黑白风格化,然后用PS放大图片,将各...
标签: Web开发
   //导入JAVA类包 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; class EditFile { //默认构造函数 EditFile(){} //保存文件函数 void SaveFile(String name,String str) { try { //声明文件输出对象 Fi...

经验教程

917

收藏

18
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部