用css实现星级评分效果


以前看到其他网站实现的实现的星级评分效果,没在意那么多,直到昨天做一个页面需要这个功能效果时,发现自己实现起来还是这么困难,折腾了半天才弄出来。

1. 其实主要是利用背景图片的切换位置来实现星级的效果,

2.分为三个层面,分别是空分层,分数层和打分层,我是用position:absolute;属性实现三个层面的定位;

3.利用链接中的a:hover属性设置背景图片来实现打分的效果;

4.为5个空链接层,定位,设置宽度(主要是控制图片的显示的个数),然后用z-index来定显示的不同顺序,使有的图层不会被遮掩。


主要实现的代码例子如下:

css代码:


<style type="text/css">
*{ margin:0; padding:0;}
.box{ width:400px; margin:10px auto;}
.listBox,.listBox .currentRating,.listBox a:hover,.listBox a:active,.listBox a:focus{ position:relative; background:url(images/star.gif) left top;}
.listBox{ position:relative; height:30px; width:150px; list-style:none; overflow:hidden; }
.listBox li{ display:inline; }
.listBox .currentRating,.listBox a{ position:absolute; left:0; top:0; height:30px; line-height:30px; text-indent:-9999px; display:block;}
.listBox a:hover,.listBox a:active,.listBox a:focus{ background-position: left center;}
.listBox a.one{ width:30px; z-index:5;}
.listBox a.two{ width:60px; z-index:4;}
.listBox a.three{ width:90px; z-index:3;}
.listBox a.four{ width:120px; z-index:2;}
.listBox a.five{ width:150px; z-index:1;}
.listBox .currentRating{ background-position:left bottom; width:50%; z-index:0;}
</style>


html代码:

<div class="box">
<ul class="listBox" >
<li class="currentRating">current</li>
<li ><a class="one" href="">1</a></li>
<li><a class="two" href="">1</a></li>
<li ><a class="three" href="">1</a></li>
<li><a class="four" href="">1</a></li>
<li ><a class="five" href="">1</a></li>
</ul>
</div>


效果:

第二种实现的方法。html代码如上:

css代码:

<style type="text/css">
* {
margin:0;
padding:0;
}
.box {
width:400px;
margin:10px auto;
}
.listBox, .listBox .currentRating, .listBox a:hover {
background:url(images/star.gif) left top;
}
.listBox {
position:relative;
height:30px;
width:150px;
list-style:none;
overflow:hidden;
}
.listBox li {
display:inline;
}
.listBox .currentRating {
position:absolute;
left:0;
top:0;
height:30px;
line-height:30px;
text-indent:-9999px;
display:block;
overflow:hidden;
border: none;
}
.listBox li a {
position:absolute;
left:0;
top:0;
height:30px;
line-height:30px;
text-indent:-9999px;
display:block;
overflow:hidden;
border: none;
width:30px;
z-index:20;
}
.listBox li a:hover {
background:url(images/star.gif) left center;
z-index:2;
left:0;
}
.listBox a.one {
left:0
}
.listBox a.one:hover {
width:30px;
}
.listBox a.two {
left:30px;
}
.listBox a.two:hover {
width:60px;
}
.listBox a.three {
left:60px;
}
.listBox a.three:hover {
width:90px;
}
.listBox a.four {
left:90px;
}
.listBox a.four:hover {
width:120px;
}
.listBox a.five {
left:120px;
}
.listBox a.five:hover {
width:150px;
}
.listBox .currentRating {
background-position:left bottom;
width:50%;
z-index:1;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值