首页  韩国资源  酷站加油  我的展厅  设计名站  古典元素  推荐下载  设计欣赏  每周专访  招募精英  人才专区  网页教程  平面设计  编程开发  设计竞赛
当前位置:首页 > 网页教程 > HTMLCSS教程 > 正文
Google
css星级效果总结
来源:蓝色理想 作者:Satellite 2006年09月22日 23:02 网友评论:0条 点击:

用纯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-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网页设计技巧吗   下一篇:实例解析Js+XML的操作方法
收藏此页】【打印】【关闭
 相关文章  我要点评

免责声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。对本文有任何异议,请联络:68design#163.com
转载要求:作者及来源信息必需保留。转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印。



关于我们 | 在线反馈 | 广告报价 | 友情链接 | 联系我们 | 免责声明 | 在线投稿 | 网站地图
Copyright © 2003-2007 68design.net, All Rights Reserve 【找网页设计师,当然上网页设计师联盟】