Js star rating effect

  • 2020-03-30 03:35:56
  • OfStack

HTML is as follows:


<div class="starts">
 <ul id="pingStar">
  <li rel="1" title=" Very bad. Here 1 points "></li>
  <li rel="2" title=" Is very poor, give 2 points "></li>
  <li rel="3" title=" Just so-so. Here 3 points "></li>
  <li rel="4" title=" Very good, to 4 points "></li>
  <li rel="5" title=" Very good. Here you are 5 points "></li>
  <span id="dir"></span>
 </ul>
 <input type="hidden" value="" id="startP">
</div>

CSS styles:


.starts,.starts ul{float:left;}
.starts{padding-left:16px;padding-top:7px;}
.starts ul li{width:32px;height:31px;float:left;background:#ddd;padding-right:3px;}
.starts ul li.on{background:red;}
.starts ul span{display:inline;float:left;padding-left:10px;height:31px;line-height:31px;}

Finally, js call is as follows:


window.onload = function () {
 var s = document.getElementById("pingStar"),
  m = document.getElementById('dir'),
  n = s.getElementsByTagName("li"),
  input = document.getElementById('startP'); //Save the selected value
 clearAll = function () {
  for (var i = 0; i < n.length; i++) {
   n[i].className = '';
  }
 }
 for (var i = 0; i < n.length; i++) {
  n[i].onclick = function () {
   var q = this.getAttribute("rel");
   clearAll();
   input.value = q;
   for (var i = 0; i < q; i++) {
    n[i].className = 'on';
   }
   m.innerHTML = this.getAttribute("title");
  }
  n[i].onmouseover = function () {
   var q = this.getAttribute("rel");
   clearAll();
   for (var i = 0; i < q; i++) {
    n[i].className = 'on';
   }
  }
  n[i].onmouseout = function () {
   clearAll();
   for (var i = 0; i < input.value; i++) {
    n[i].className = 'on';
   }
  }
 }
}

Check the demo (link: http://demo.jb51.net/js/2014/jsxxdf/)

If you like to use jquery based star rating effect, you can refer to this address:


Related articles: