首页 | 网页教程 | 网站模板 | 网页特效 | 矢量图库 | 字体下载 | ASP源码下载 | 酷站欣赏 | FLASH源码 | 有求必应
网页教程导航
·photoshop教程
·flash教程
·asp教程
·fireworks教程
·dreamweaver教程
·搜索引擎
·html/css教程
·3dmax教程
·java教程
·php教程
·FrontPage教程
 
 
 
首页 >> 网页教程 >> html/css教程 >> 正文
CSS实现星级效果总结
 责任编辑:川北素材网 添加日期:08-04-22 11:52:54 浏览:
  用纯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不会遮挡住其他链接);

  也许上面这段文字你看得有些生涩,没有关系,让我们结合css代码来看看解决办法。
<ul class="star-rating">
<li class="current-rating">Currently 3.5/5 Stars.</li>
<li><a href="#" title="1 star out of 5" class="one-star">1</a></li>
<li><a href="#" title="2 stars out of 5" class="two-stars">2</a></li>
<li><a href="#" title="3 stars out of 5" class="three-stars">3</a></li>
<li><a href="#" title="4 stars out of 5" class="four-stars">4</a></li>
<li><a 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{/*设置li的浮动属性*/
padding:0px;
margin:0px;
/*\*/
float: left;
/* */
}
.star-rating li a{/*设置a的布局为绝对布局和垂直坐标并隐藏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{/*设置a:hover的背景图片为打分星/垂直坐标/left为0,注意垂直坐标一定要小于a的垂直坐标*/
background: url(star_rating2.gif) left center;
z-index: 2;
left: 0px;
}
/*以下5个class用来设置5个链接的位置和hover的宽度*/
.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-

9 7 3 1 2 4 8 :
打印本页 | 加入收藏
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 管理登陆
版权所有:川北素材 为广大站长提供素材支持,业务联系QQ:9987142
Copyright © 2008 www.chuanbei.net All Rights Reserved.