css星级效果总结

来源:互联网 发布:金庸小说武功排名知乎 编辑:程序博客网 时间:2024/04/28 18:22
 <ul class="star-rating">
 
<li class="current-rating">Currently 3.5/5 Stars.</li>
 
<li><href="#" title="1 star out of 5" class="one-star">1</a></li>
 
<li><href="#" title="2 stars out of 5" class="two-stars">2</a></li>
 
<li><href="#" title="3 stars out of 5" class="three-stars">3</a></li>
 
<li><href="#" title="4 stars out of 5" class="four-stars">4</a></li>

 
<li><href="#" title="5 stars out of 5" class="five-stars">5</a></li>
 
</ul>
 
 
<style>
.star-rating
{
list-style
:none;
margin
: 0px;
padding
:0px;
width
: 150px;
height
: 30px;
position
: relative;
background
: url(star_rating2.gif) top left repeat-x; 
}

.star-rating li
{
padding
:0px;
margin
:0px;
/**/
float
: left;
/* */
}

.star-rating li a
{
display
:block;
width
:30px;
height
: 30px;
text-decoration
: none;
text-indent
: -9000px;
z-index
: 20;
position
: absolute;
padding
: 0px;
}

.star-rating li a:hover
{
background
: url(star_rating2.gif) left center;
z-index
: 2;
left
: 0px;
}


.star-rating a.one-star
{
left
: 0px;
}

.star-rating a.one-star:hover
{
width
:30px;
}

.star-rating a.two-stars
{
left
:30px;
}

.star-rating a.two-stars:hover
{
width
: 60px;
}

.star-rating a.three-stars
{
left
: 60px;
}

.star-rating a.three-stars:hover
{
width
: 90px;
}

.star-rating a.four-stars
{
left
: 90px;
}
 
.star-rating a.four-stars:hover
{
width
: 120px;
}

.star-rating a.five-stars
{
left
: 120px;
}

.star-rating a.five-stars:hover
{
width
: 150px;
}


.star-rating li.current-rating
{
background
: url(star_rating2.gif) left bottom;
position
: absolute;
height
: 30px;
width
:105px;
display
: block;
text-indent
: -9000px;
z-index
: 1;
}
 
 
</style>
 

用纯css打造星级评分效果正在被越来越多地应用在网络RIA中,结合ajax等技术,可以渲染出很出色的视觉效果和很棒的用户体验,在这篇文章开始之前,大家可以先去cssheaven感受一下。

最近由于项目需要,我在网上找了很多css星级评分的例子和说明,但是发现大多数都是翻译国外的文章,而且解释得并不是非常清楚,所以我决定自己来做一个总结,也希望能够给大家一些帮助。

首先用中文写一下这个效果的算法:
1. 使用背景图片的位置切换来获得星级效果;

2. 整个效果最关键的地方就是“三层理论”,整个效果分为三层——空分层、分数层和打分层,三层的布局均为absolute,以避免ul本身自带的相对布局(当然用div也可以获得同样效果);
3. 空分层就是使用背景图片中的“空星”作为背景,并横向平铺;
4. 分数层的宽度等于(分数*图片宽度)得到的数值,并且使用背景图片中的“分数星(例子中为黄色)”作为背景横向平铺;
5. 打分层就是将5个空链接置于5个星星的位置上(宽度要和背景图片吻合),并将5个a:hover的背景设为“打分星(这里为绿色)”,宽度设为星数*图片 宽度,left为0(靠左,这样结合a:hover不同的宽度就可以出现打分效果),垂直坐标小于a的垂直坐标(以确保当前a:hover不会遮挡住其他 链接);

原创粉丝点击