JavaScript star rating Effects (handwriting) _javascript tips

Source: Internet
Author: User
This morning I took the time to write a star rating effect, to share with you. Because of the limited level, please point out if you have any questions.
First to prepare a picture of the stars, Gray is the default state, yellow is the selection state. As shown in figure:
finally attach code
Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>javascript star rating </title>
<style type= "Text/css" >
*{margin:0;padding:0;}
. wrapper{height:20px;padding:5px;width:130px;margin:100px auto 10px;}
A{float:left;width:26px;height:20px;background:url (star.png) 0-20px no-repeat;}
P{font:24px Simsun;width:130px;margin-left:auto;margin-right:auto;}
</style>
<body>
<div class= "wrapper" >
<a href= "javascript:;" ></a>
<a href= "javascript:;" ></a>
<a href= "javascript:;" ></a>
<a href= "javascript:;" ></a>
<a href= "javascript:;" ></a>
</div>
<p></p>
</body>
<script type= "Text/javascript" >
Window.onload = function () {
var star = document.getElementsByTagName (' a ');
var odiv = document.getelementsbytagname (' div ') [0];
var temp = 0;
for (var i = 0, len = star.length i < len; i++) {
Star[i].index = i;
Star[i].onmouseover = function () {
Clear ();
for (var j = 0; J < This.index + 1; j + +) {
star[j].style.backgroundposition = ' 0 0 ';
}
}
Star[i].onmouseout = function () {
for (var j = 0; J < This.index + 1; j + +) {
star[j].style.backgroundposition = ' 0-20px ';
}
Current (temp);
}
Star[i].onclick = function () {
temp = This.index + 1;
document.getElementsByTagName (' P ') [0].innerhtml = temp + ' star ';
Current (temp);
}
}
Clear All
function Clear () {
for (var i = 0, len = star.length i < len; i++) {
star[i].style.backgroundposition = ' 0-20px ';
}
}
Show the current number of stars
function current (temp) {
for (var i = 0; i < temp; i++) {
star[i].style.backgroundposition = ' 0 0 ';
}
}
};
</script>

Attached download Address
PS: This is my idle boring, through their own knowledge of JavaScript, random write some effects.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.