JavaScript实现星级评价效果

图片是width:36px; height:25px;

背景是白色,中间一个空的五角星,空的边缘有一像素的边框;(不然就看不到了);

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>星级评价(可半星)</title> <style> .xin{height: 25px; background: url(images/x.png); position: relative; display: inline-block;} .xin span{display: block; height: 25px; background: #f60; position: absolute; left: 0; top: 0;z-index: -1;transition: 0.2s;} .xin ul{position: absolute; top: 0; left: 0; height: 25px; margin: 0; padding: 0; list-style: none;} .xin ul li{float: left; width: 18px; height: 25px;} .fensu{display: inline-block;} </style> </head> <body> <input type="hidden" title="10" value="3.5"> <input type="hidden" title="5" value="1.5"> <script> var input=document.querySelectorAll("input[type=hidden]"); input.forEach(function(_input){ var Div=document.createElement("div"); Div.className="xin"; Div.style.width=18*(_input.title || 5)*2 + "px"; _input.parentNode.insertBefore(Div,_input); var span=document.createElement("span"); span.style.width=(_input.value)*36+"px"; var p=document.createElement("p"); p.innerHTML=_input.value+"分"; p.className="fensu"; Div.parentNode.insertBefore(p,Div); Div.parentNode.insertBefore(Div,p); Div.appendChild(span); var ul=document.createElement("ul"); Div.appendChild(ul); for(var i=0;i<(_input.title || 5)*2;i++){ var li=document.createElement("li"); li.title=(i+1)/2+"分"; ul.appendChild(li); } var LI=ul.querySelectorAll("li"); LI.forEach(function(_li,x){ _li.onclick=function(){ _input.value=(x+1)/2; span.style.width=18*(x+1)+"px"; p.innerHTML=_input.value+"分"; } _li.onmouseover=function(){ span.style.width=18*(x+1)+"px"; } _li.onmouseout=function(){ span.style.width=(_input.value)*36+"px"; } }) var br=document.createElement("br"); p.parentNode.insertBefore(br,p); p.parentNode.insertBefore(p,br); }) </script> </body> </html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/260363124ec5f4c0d83a1969e8e86a2a.html