2015-09-04 64 views
3

我正在使用無線電的css星級評分系統。 但是我似乎無法弄清楚我在這裏做錯了什麼......它似乎突出顯示了當我點擊時......但它應該突出顯示當我懸停時。具有單選按鈕的CSS星級

<fieldset>  
<input type="radio" id="star-5" name="rating-01" value="5"><label for="star-5"></label> 
    <input type="radio" id="star-4" name="rating-01" value="4"><label for="star-4"></label> 
    <input type="radio" id="star-3" name="rating-01" value="3"><label for="star-3"></label> 
    <input type="radio" id="star-2" name="rating-01" value="2"><label for="star-2"></label> 
    <input type="radio" id="star-1" name="rating-01" value="1"><label for="star-1"></label> 
</fieldset> 


input[type=radio] { display: none; visibility: collapse; } 

input[type=radio] + label { 
    position: relative; float: right; clear: none; display: block; 
    width: 18.4%; height: 120px; margin: 0 1% 0 1%; padding: 0; 
    outline: 0; cursor: pointer; 

    background-color: green; 
} 
input[type=radio]#star-1 + label { margin: 0 1% 0 0; } input[type=radio]#star-5 + label { margin: 0 0 0 1%; } 

input[type=radio]:hover + label, 
    input[type=radio]:hover + label ~ input[type=radio] + label { 
     background-color: red; 
} 


UPDATE


原來這就是我設法到目前爲止做的:JSFiddle;

CSS:

input[type=radio] { display: none; visibility: collapse; } 

input[type=radio] + label { 
    position: relative; float: right; clear: none; display: block; 
    width: 18.4%; height: 120px; margin: 0 1% 0 1%; padding: 0; 
    outline: 0; cursor: pointer; background-color: green; 
} 
input[type=radio]#star-1 + label { margin: 0 1% 0 0; } input[type=radio]#star-5 + label { margin: 0 0 0 1%; } 

fieldset > input[type=radio]:checked ~ label { 
    background-color: blue; 
} 

fieldset:hover > input[type=radio] + label:hover, 
fieldset:hover > input[type=radio] + label:hover ~ label { 
    background-color: gold; 
} 

但是仍然有一個問題...當您選擇框和他們去藍色。而你將鼠標懸停在藍色的支架上,而懸停的部分則是黃色的。

將鼠標懸停後,如何重置藍色?

+0

https://jsfiddle.net/5ntpom7a/對我來說,這是行得通的... –

+0

@SimonHänisch一旦你點擊並突出顯示,然後刪除光標並嘗試再次單擊它將無法正常工作。它也應該突出':懸停'不點擊..甚至不知道它爲什麼點擊工作,因爲我沒有編程::檢查' – Borsn

+0

對我來說沒有任何反應,點擊... –

回答

1

在這裏,你去。可能是您可以想到的最簡單的純CSS解決方案。

HTML

<div class="stars"> 
    <input type="radio" id="rate-5" name="rating-1"> 
    <label for="rate-5"></label> 
    <input type="radio" id="rate-4" name="rating-1"> 
    <label for="rate-4"></label> 
    <input type="radio" id="rate-3" name="rating-1"> 
    <label for="rate-3"></label> 
    <input type="radio" id="rate-2" name="rating-1"> 
    <label for="rate-2"></label> 
    <input type="radio" id="rate-1" name="rating-1"> 
    <label for="rate-1"></label> 
</div> 

CSS

.stars { 
    float: left; 
    overflow: hidden; 
    width: 100%; 
} 
.stars input[type=radio]:checked ~ label:after { 
    background: blue; 
} 
.stars input[type=radio] { 
    display: none; 
} 
.stars input[type=radio]:first-child + label { 
    padding-right: 0; 
} 
.stars:hover input[type=radio]:checked ~ label:after, 
.stars label:after { 
    background: green; 
} 
.stars label { 
    box-sizing: border-box; 
    width: 20%; 
    padding-right: 2%; 
    height: 120px; 
    float: right; 
    cursor: pointer; 
} 
.stars label:after { 
    display: block; 
    content: ""; 
    height: 120px; 
    width: 100%; 
    float: right; 
    transition: all 0.25s; 
    background: green; 
} 
.stars label:hover:after, 
.stars label:hover ~ label:after { 
    background: gold !important; 
} 

https://jsfiddle.net/rsyfchfo/

希望這有助於:)

[更新]增加了 「重置懸停選擇」。

[更新2]在星星之間的空白處添加懸停。

+0

有趣的是,當然......但是如果你選擇返回,那麼網格不會在':hover'上重置 – Borsn

+1

@Boris是這樣的嗎? https://jsfiddle.net/yLc4cmq3/ –

+0

多數民衆贊成在完美!還有一個問題......當你將鼠標懸停在空白處時,它似乎重置了所有內容。有沒有辦法阻止? – Borsn

2

很久以前我創造了一些東西。計算總票數也有javascript

Fiddle example

一小段代碼片段:

function showme(id) { 
 
    document.getElementById('result').innerHTML = 'My vote : '+id.value; 
 
    var r = document.getElementById('res').value.split(','); 
 
    var s=''; 
 
    for(var i=0;i<5;i++) { 
 
     if(id.value==(i+1).toString()) { 
 
     var x = parseInt(r[i])+1; 
 
     r[i]=x.toString(); 
 
     } 
 
     if(i==4) {s+=r[i];} else {s+=r[i]+',';} 
 
    } 
 
    document.getElementById('res').value=s; 
 
    calc(); 
 
    } 
 
    function calc() { 
 
    var x=document.getElementById('res').value.split(','); 
 
    var r=0; 
 
    var t=0; 
 
    for(var i=0;i<5;i++) { 
 
     t+=parseInt(x[i]); 
 
     r+=(parseInt(x[i])*(i+1)); 
 
    } 
 
    var s=parseInt((r/t)*20); 
 
    document.getElementById('bar').style.width=s.toString()+'%'; 
 
    document.getElementById('sta').innerHTML=s.toString()+'%'; 
 
    }
#divRateCnt { 
 
    width: 130px; 
 
    position: relative; 
 
} 
 

 
#divRareCnt, #divStarsCnt{ 
 
    height: 26px; 
 
    background: url(http://s4.postimg.org/wi683zp2h/stars.png) 0 0px repeat-x; 
 
} 
 

 
#divRateCnt input{ 
 
    display: none; 
 
} 
 

 
#divRateCnt label{ 
 
    display: none; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    height: 26px; 
 
    width: 130px; 
 
    cursor: pointer; 
 
} 
 
#divRateCnt:hover label{ 
 
    display: block; 
 
} 
 
#divRateCnt label:hover{ 
 
    background: url(http://s4.postimg.org/wi683zp2h/stars.png) 0 -52px repeat-x; 
 
} 
 

 
#divRateCnt label + input + label{width: 104px;} 
 
#divRateCnt label + input + label + input + label{width: 78px;} 
 
#divRateCnt label + input + label + input + label + input + label{width: 52px;} 
 
#divRateCnt label + input + label + input + label + input + label + input + label{width: 26px;} 
 

 
#divRateCnt input:checked + label{ 
 
    display: block; 
 
    background: url(http://s4.postimg.org/wi683zp2h/stars.png) 0 -52px repeat-x; 
 
}
<input type="hidden" value="0,0,0,0,0" id="res" /> 
 
<div id="divRateCnt"> 
 
    <div id="divStarsCnt"></div> 
 
    <input type="radio" name="rating" id="star5" value="5" onchange="showme(this);"><label for="star5"></label> 
 
    <input type="radio" name="rating" id="star4" value="4" onchange="showme(this);"><label for="star4"></label> 
 
    <input type="radio" name="rating" id="star3" value="3" onchange="showme(this);"><label for="star3"></label> 
 
    <input type="radio" name="rating" id="star2" value="2" onchange="showme(this);"><label for="star2"></label> 
 
    <input type="radio" name="rating" id="star1" value="1" onchange="showme(this);"><label for="star1"></label> 
 
</div> 
 
<br> 
 
<span id="result"></span> 
 
<br><br> 
 
Total vote(s) :<br> 
 
<div style="width:130px;height:26px;background:url(http://s4.postimg.org/wi683zp2h/stars.png) 0 0 repeat-x;position:relative;"> 
 
    <div id="bar" style="width:130px;height:26px;background:url(http://s4.postimg.org/wi683zp2h/stars.png) 0 -26px repeat-x;position:absolute;top:0;left:0;width:0%;"></div> 
 
</div> 
 
<span id="sta"></span>

+0

你能查看我更新的問題嗎?不需要JS計算...它會轉到數據庫。 – Borsn

2

https://jsfiddle.net/ypne7t2w/1/

此復位徘徊字段集時,標籤顏色,但不針對標籤懸停:

fieldset:hover > input[type=radio] + label, 
fieldset:hover > input[type=radio] + label ~ label { 
    background-color: green; 
} 
+0

迄今爲止效果很好。但是在想,因爲我會用圖像來表示星星。如果藍色和黃金在那裏,我將不得不使用相同的圖像兩次,這只是增加了CSS。我嘗試將藍色和黃色結合在一起,我現在得到了這個:https://jsfiddle.net/ypne7t2w/2/整個字段集在懸停時重置。有什麼辦法解決這個問題? – Borsn

+0

兩次使用相同的圖像url不會使瀏覽器加載兩次。一旦加載圖像在你的網站上隨處可見,只要url不變(瀏覽器將緩存它)...或者你的意思是說你的css文件的大小增加了,因爲「url(file.jpg)」是比「綠色」長几個字?我不認爲這些字節會對您的頁面加載時間產生任何明顯的影響。 –