I found great CSS just a way to make freezes . Saving stars highlighted will still require some javascript, but much less.
The general idea is that you have a background image (star) for ul with a set length (in your case, 5 times the image width, 5 stars) and repeat the image along the x axis ( background: url(star.gif) repeat-x; ). Each <a> has a different width (20%, 40%, 60%, 80%, 100%, for 5 stars) and is placed in a different z-index.
This updates the authorβs idea that is a little closer to your use case:
HTML:
<div class='rate_widget'> <ul> <li><a href='#' class='one-star'>1</a></li> <li><a href='#' class='two-stars'>2</a></li> <li><a href='#' class='three-stars'>3</a></li> <li><a href='#' class='four-stars'>4</a></li> <li><a href='#' class='five-stars'>5</a></li> </ul> </div>
CSS
.rate_widget ul{ background: url(star_empty.png) repeat-x; } .rate_widget a:hover{ background: url(star_highlight.png) repeat-x; } .rate_widget a:active, .rate_widget a:focus, .rate_widget a.checked{ background: url(star_full.png) repeat-x; } .rate_widget ul{ position:relative; width:125px; height:25px; overflow:hidden; list-style:none; margin:0; padding:0; background-position: left top; } .rate_widget li{ display: inline; } .rate_widget a{ position:absolute; top:0; left:0; text-indent:-1000em; height:25px; line-height:25px; outline:none; overflow:hidden; border: none; } .rate_widget a.one-star{ width:20%; z-index:6; } .rate_widget a.two-stars{ width:40%; z-index:5; } .rate_widget a.three-stars{ width:60%; z-index:4; } .rate_widget a.four-stars{ width:80%; z-index:3; } .rate_widget a.five-stars{ width:100%; z-index:2; } .rate_widget a.checked{ z-index:1; }
JavaScript:
$('.rate_widget a').click(function(){ // make sure the chosen star stays selected var star = $(this); star.closest('ul').find('.checked').removeClass('checked'); star.addClass('checked'); //whatever else you want to do when something gets clicked });
And here is the fiddle to see this in action: http://www.jsfiddle.net/BHaTu/
source share