DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
純 CSS 畫出五角星與星級評分元件
編輯:CSS詳解     

星級評分是評論區、商品頁最常見的互動元件之一。本文示範完全不使用圖片的做法:先用 border 拼出一顆五角星,再用 CSS 精靈圖與 hover 完成可視化的評分條。互動層(點擊打分、送出後回寫後端)仍需 JavaScript 補上,這裡只談畫面本身。

用 border 拼一顆五角星

原理是把元素的寬高歸零,靠四條不同顏色的粗邊框互相切割出三角形,再用 transform 旋轉組合。主體是一個向上的三角形,:before 補左下角,:after 補右下角。

#star-five {
margin: 50px 0;
position: relative;
display: block;
color: red;
width: 0px;
height: 0px;
border-right: 100px solid transparent;
border-bottom: 70px solid red;
border-left: 100px solid transparent;
-moz-transform: rotate(35deg);
-webkit-transform: rotate(35deg);
-ms-transform: rotate(35deg);
-o-transform: rotate(35deg);
}
#star-five:before {
border-bottom: 80px solid red;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
position: absolute;
height: 0;
width: 0;
top: -45px;
left: -65px;
display: block;
content: "";
-webkit-transform: rotate(-35deg);
-moz-transform: rotate(-35deg);
-ms-transform: rotate(-35deg);
-o-transform: rotate(-35deg);
}
#star-five:after {
position: absolute;
display: block;
color: red;
top: 3px;
left: -105px;
width: 0px;
height: 0px;
border-right: 100px solid transparent;
border-bottom: 70px solid red;
border-left: 100px solid transparent;
-webkit-transform: rotate(-70deg);
-moz-transform: rotate(-70deg);
-ms-transform: rotate(-70deg);
-o-transform: rotate(-70deg);
content: "";
}

改用精靈圖做評分條

純 border 版本很酷,但五顆星寫五份標籤太囉嗦。實務上更省事的作法是做一張直向排列的精靈圖,把 0 到 5 星共六種狀態縱向排好,再用 background-position 切出來。

Demo 使用的圖檔如下,可右鍵另存:

HTML 結構

<ul class="rating nostar">
<li class="one"><a href="#" title="1 Star">1</a></li>
<li class="two"><a href="#" title="2 Stars">2</a></li>
<li class="three"><a href="#" title="3 Stars">3</a></li>
<li class="four"><a href="#" title="4 Stars">4</a></li>
<li class="five"><a href="#" title="5 Stars">5</a></li>
</ul>

CSS

.rating {
width: 124px;
height: 19px;
margin: 0 0 20px 0;
padding: 0;
list-style: none;
clear: both;
position: relative;
background: url(stars.png) no-repeat 0 0;
}
.nostar { background-position: 0 0 }
.onestar { background-position: 0 -19px }
.twostar { background-position: 0 -38px }
.threestar { background-position: 0 -57px }
.fourstar { background-position: 0 -76px }
.fivestar { background-position: 0 -95px }
ul.rating li {
cursor: pointer;
float: left;
text-indent: -999em;
}
ul.rating li a {
position: absolute;
left: 0;
top: 0;
width: 25px;
height: 19px;
text-decoration: none;
z-index: 200;
}
ul.rating li.one a { left: 0 }
ul.rating li.two a { left: 25px }
ul.rating li.three a { left: 50px }
ul.rating li.four a { left: 75px }
ul.rating li.five a { left: 100px }
ul.rating li a:hover {
z-index: 2;
width: 125px;
height: 19px;
overflow: hidden;
left: 0;
background: url(stars.png) no-repeat 0 0
}
ul.rating li.one a:hover { background-position: 0 -19px; }
ul.rating li.two a:hover { background-position: 0 -38px; }
ul.rating li.three a:hover { background-position: 0 -57px }
ul.rating li.four a:hover { background-position: 0 -76px }
ul.rating li.five a:hover { background-position: 0 -95px }

這裡有兩個關鍵設計:每個 <a> 都用絕對定位疊在同一條上,靠 left 決定各自的熱區;hover 時把該元素寬度撐到 125px 並提高 z-index,讓它蓋住左側所有星等,一次顯示對應的星級。後續只要補上 click 事件與選中狀態的 class 切換,就是一個完整的評分元件了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved