How To: Create Star rating using CSS Sprite and Jquery – With Demo


As we all know CSS sprite and jquery are in trend. I have seen many star rating tricks but none of them are using css-sprite. I tried to make one , hope you all will love it. Moreover i have used ajax call to store the voting value.

DEMO

So lets start with HTML. Simply paste following HTML just inside your body tag:

1
2
3
4
5
6
7
<div id="ratingdiv">
		<a href="#" class="undone" rel="1">star one</a>
		<a href="#" class="undone" rel="2">star two</a>
		<a href="#" class="undone" rel="3">star three</a>
		<a href="#" class="undone" rel="4">star four</a>
		<a href="#" class="undone" rel="5">star five</a>
</div>

.

Here comes the tricky CSS. Paste it in your stylesheet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ratingdiv{overflow:hidden}
#ratingdiv .undone, #ratingdiv .fade{
background:url("star.gif") -30px 0 no-repeat;
width:28px;
display:block;
height:30px;
float:left;
margin-right:4px;
text-indent:-99999px
}
#ratingdiv .done{
background:url("star.gif") no-repeat;
width:28px;
display:block;
height:30px;
float:left;
margin-right:4px;
text-indent:-99999px
}
#ratingdiv .undone:hover{background-position:0}

.

Make a new php file and name it as “rate.php”. Paste the following code in it:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
	for($i=1;$i<=5;$i++)
	{
		if($i <= $_POST['rate'])
			$class="done";
		else
			$class="fade";
	?>
		<a href="#" class="<?php echo $class ?> voted">star rated</span>
	<?php
	}
?>

.

And in last paste the following Jquery code inside your head tag of your document:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/JavaScript">
$(document).ready(function (){
	$('#ratingdiv .undone').click(function(){
		var div = '#ratingdiv';	
		$(div).html('<img src="load.gif" />');
		var postdata = "rate=" + $(this).attr('rel');
		$.ajax({type: "POST",url: "rate.php",data: postdata,success: function(msg){$(div).html(msg)}});
	});	
	$('#ratingdiv .voted').live('click' , function(){
		alert('Already Done!');
	});
});
</script>

.

All Done!!

Check out the DEMO

Cheers!

Complete set of 642-456 questions and E20-361 practice tests to guide you in exact way so you will pass 1Y0-A18 on first try.

Comments

01.17.10

Hi! I really liked your forum, especially this section. I just signed up and immediately decided to introduce myself, if I’m wrong section, ask the moderators to move the topic to the right place, hopefully it will take me well… My name is Mary, me 29 years, humourist and serious woman in one person. I apologize for my English

01.17.10

All-Nighters: Mind Games
Jonah Lehrer explores how the nature of the human mind can make it an enemy of sleep.

01.17.10

well, thanks, this tutorial was helpful.

01.17.10

Very good article. Quite informative and well written. I enjoyed reading it and it made me think.

01.17.10

I enjoyed reading this article. I will come back soon to read it again.

Wow! Thank you! I always wanted to write on my blog something like that. Can I implement a fragment of your post to my website?

css sprite seems to be interesting, I´ll have to try that

01.17.10

thank you. nice blog..

01.17.10

Informations like this I always prefer to collect. I always open your post with great wish.

01.17.10

Wow, that’s quite good; clean, sleek and simple

I have been looking through your site. You have some nice posts on here, especially this one, I enjoyed it…nice post. Consider yourself bookmarked

01.17.10

It has lots of information, I like the creation, the designs, keep up the good work.

01.17.10

I have read your article, I think this is very good! Simple language, concise blog! Another kind of style! I like it!

01.17.10

This is quite useful used it many times, and its easy too.

ohh…..really i dont believe this