用css制作星级评分
原文:creating a star rater using css 链接:http://komodomedia.com/blog/index.php/2005/08/24/creating-a-star-rater-using-css/ 版权:版权归原作者所有,翻译文档版权归greengnn所有。
第一个模型中忽视了半星级的情况和无初始的星级,下来我们就是要解决这个问题。 现回顾一下第一个模型,文章地址 对这篇文章不理解的建议去看看用css制作星级评分i
step 1: 先看看效果 | heck it in action
step 2: the xhtml
<ul class="star-rating"> <li class="current-rating">currently 3.5/5 stars.</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul>
和第一个模型的结构相似,唯一不同的是: <li class="current-rating">currently 3.5/5 stars.</li> 定义初始值
step 3: the star image
我们制作一个有三个星的图片,第一个星是空值,第二个是要选择的值,第三个是真实的值。
step 4: the css, the magic
.star-rating li.current-rating { background: url(star_rating.gif) left bottom; position: absolute; height: 30px; display: block; text-indent: -9000px; z-index: 1; } 定义了初始值,为了避免继承容器ul的相对定位,采用position: absolute;每个星的高度为height:30px;别的就是隐藏文本和定义对齐方式。
空值css:
.star-rating { … background: url(star_rating.gif) top left repeat-x; }
选择值css:
.star-rating li a:hover { background: url(star_rating.gif) left center; … } 初始值当然会随着选择变动,那么怎样实现它的变化呢?
<li class="current-rating" style="width:105px;">currently 3.5/5 stars.</li> 看了这段代码相信你就知道是什么原因了!那这个width是怎样计算的呢?
average rating|平均值: 3.5 each star width|每个星的宽度: 30px; set width to|将宽度设为: 3.5 * 30 = 105px
下面欣赏一下这个新模型吧?
example 1:
Resources: Both the XHTML and the CSS are provided below.
xhtml <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> css .star-rating{ list-style:none; margin: 0px; padding:0px; width: 150px; height: 30px; position: relative; background: url(images/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(images/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(images/star_rating2.gif) left bottom; position: absolute; height: 30px; width:105px; display: block; text-indent: -9000px; z-index: 1; }
example 2
Resources: Both the XHTML and the CSS are provided below.
xhtml <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> css .star-rating{ list-style:none; margin: 0px; padding:0px; width: 125px; height: 25px; position: relative; background: url(images/alt_star.gif) top left repeat-x; } .star-rating li{ padding:0px; margin:0px; /*\*/ float: left; /* */ } .star-rating li a{ display:block; width:25px; height: 25px; text-decoration: none; text-indent: -9000px; z-index: 20; position: absolute; padding: 0px; } .star-rating li a:hover{ background: url(images/alt_star.gif) left bottom; z-index: 2; left: 0px; } .star-rating a.one-star{ left: 0px; } .star-rating a.one-star:hover{ width:25px; } .star-rating a.two-stars{ left:25px; } .star-rating a.two-stars:hover{ width: 50px; } .star-rating a.three-stars{ left: 50px; } .star-rating a.three-stars:hover{ width: 75px; } .star-rating a.four-stars{ left: 75px; } .star-rating a.four-stars:hover{ width: 100px; } .star-rating a.five-stars{ left: 100px; } .star-rating a.five-stars:hover{ width: 125px; } .star-rating li.current-rating{ background: url(images/alt_star.gif) left center; position: absolute; height: 25px; display: block; text-indent: -9000px; z-index: 1; }
/* remove halo effect in firefox */ a:active{ outline: none; }
example 3:
Resources: Both the XHTML and the CSS are provided below.
xhtml <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> css .star-rating{ list-style:none; margin: 0px; padding:0px; height: 125px; width: 25px; position: relative; background: url(images/vert_star.gif) top left repeat-y; } .star-rating li{ padding:0px; margin:0px; float:left; } .star-rating li a{ display:block; height:25px; width: 25px; text-decoration: none; text-indent: -9000px; z-index: 20; position: absolute; padding: 0px; } .star-rating li a:hover{ background: url(images/vert_star.gif) right top repeat-y; z-index: 2; top: 0px; } .star-rating a.one-star{ top: 0px; } .star-rating a.one-star:hover{ height:25px; } .star-rating a.two-stars{ top:25px; } .star-rating a.two-stars:hover{ height: 50px; } .star-rating a.three-stars{ top: 50px; } .star-rating a.three-stars:hover{ height: 75px; } .star-rating a.four-stars{ top: 75px; } .star-rating a.four-stars:hover{ height: 100px; } .star-rating a.five-stars{ top: 100px; } .star-rating a.five-stars:hover{ height: 125px; } .star-rating li.current-rating{ background: url(images/vert_star.gif) center top repeat-y; position: absolute; width: 25px; display: block; text-indent: -9000px; z-index: 1; }
/* remove halo effect in firefox */ a:active{ outline: none; } |