JS实现五星好评案例
程序员文章站
2022-07-06 14:27:38
本文实例为大家分享了js实现五星好评案例的具体代码,供大家参考,具体内容如下业务逻辑是我需要先创建出所有我需要用到的标签和样式再写出我们星星对应的行为,分数对应行为,笑脸对应行为,点击对应行为,抽象化...
本文实例为大家分享了js实现五星好评案例的具体代码,供大家参考,具体内容如下
业务逻辑是我需要先创建出所有我需要用到的标签和样式再写出我们星星对应的行为,分数对应行为,笑脸对应行为,点击对应行为,抽象化出来,方便维护。并且在点击后我们抛出事件,记录下对应的name,分数等信息,保存在cookie中。
在编写过程中,一个是位置问题,很容易出现在没有创建就进行appendchild,第二个就是在添加行为时如何调整星星,笑脸的位置。
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> </head> <body> <script type="module"> import star from "./js/star.js"; let list=["商品符合度","店家服务态度","快递配送速度","快递员服务","快递包装"] list.foreach(item => { let star = new star(item); star.appendto("body"); star.addeventlistener("change",changehandler); }); function changehandler(e){ var date=new date(); date.setmonth(11); setcookie(e.scorelist,date); function setcookie(data,date){ //json形式存储到cookie中 var str=date===undefined ? "" : ";expires="+date.toutcstring(); for(var prop in data){ var value=data[prop]; if(typeof value==="object" && value!==null) value=json.stringify(value); document.cookie=prop+"="+value+str; } } } </script> </body> </html>
export default class component{ //创建warp 和 appendto方法 elem; constructor(){ this.elem=this.createelem(); } createelem(){ if(this.elem) return this.elem; let div=document.createelement("div"); return div; } appendto(parent){ if(typeof parent==="string")parent=document.queryselector(parent); parent.appendchild(this.elem); } }
import component from "./component.js"; export default class star extends component{ label=""; score; face; starcon; static star_num=5; stararr=[]; static starscorelist=[]; pos=-1; constructor(_label){ super(); this.label=_label; object.assign(this.elem.style,{ width:"auto", height:"16px", float:"left", paddingbottom:"10px", marginright:"20px", paddingtop:"16px", }) star.starscorelist[_label]=0; this.createlable(_label); this.createstar(); this.createscore(); } createlable(_label){ let labels=document.createelement("div"); labels.textcontent=_label; object.assign(labels.style,{ float:"left", height: "16px", lineheight: "16px", marginright: "10px", overflow: "hidden", whitespace: "nowrap", textoverflow: "ellipsis", font: '12px/150% tahoma,arial,microsoft yahei,hiragino sans gb,"\u5b8b\u4f53",sans-serif', color: "#666" }); this.elem.appendchild(labels); } createstar(){ this.starcon=document.createelement("div"); object.assign(this.starcon.style,{ float:"left", height:"16px", position:"relative", margintop:"1px" }) for(let i=0;i<star.star_num;i++){ let star=document.createelement("div"); object.assign(star.style,{ backgroundimage:"url(./img/commstar.png)", width:"16px", height:"16px", float:"left", }) this.stararr.push(star); this.starcon.appendchild(star); } object.assign(this.face.style,{ width:"16px", height:"16px", backgroundimage:"url(./img/face-red.png)", position:"absolute", top:"-16px", display:"none" }); this.starcon.appendchild(this.face); this.elem.appendchild(this.starcon); this.starcon.addeventlistener("mouseover",e=>this.mousehandler(e)) this.starcon.addeventlistener("click",e=>this.mousehandler(e)) this.starcon.addeventlistener("mouseleave",e=>this.mousehandler(e)) this.face=document.createelement("div"); } createscore(){ this.score=document.createelement("span"); object.assign(this.score.style,{ position:"relative", width:"30px", height:"16px", top:"-2px", marginleft:"10px", lineheight:"16px", textalign:"right", color:"#999", font:"12px/150% tahoma,arial,microsoft yahei,hiragino sans gb,sans-serif", }); this.score.textcontent="0分"; this.starcon.appendchild(this.score); } mousehandler(e){ //鼠标行为 let index=this.stararr.indexof(e.target); switch(e.type){ case "mouseover": if(index<0) return; this.changeface(index); this.changescore(index+1); this.changestar(index+1); break; case "click": this.pos=index; this.dispatch(); break; case "mouseleave": this.changestar(this.pos+1); this.changeface(this.pos); this.changescore(this.pos+1); break; default: return; } } changestar(n){ for(let i=0;i<this.stararr.length;i++){ if(i<n){ this.stararr[i].style.backgroundpositiony="-16px"; }else{ this.stararr[i].style.backgroundpositiony="0px"; } } } changescore(n){ this.score.textcontent=n+"分"; if(n===0){ this.score.style.color="#999"; }else{ this.score.style.color="#e4393c"; } } changeface(n){ if(n<0){ this.face.style.display="none"; return; } let index=star.star_num-n-1; //这里的笑脸因为图片的原因是反向的 this.face.style.display="block"; this.face.style.backgroundpositionx=-index*20+"px"; this.face.style.left=this.stararr[n].offsetleft+"px"; } changescore(n){ this.score.textcontent=n+"分"; if(n===0){ this.score.style.color="#999"; }else{ this.score.style.color="#e4393c"; } } dispatch(){ // console.log("aaa") star.starscorelist[this.label]=this.pos+1; var evt=new event("change"); evt.score=this.pos+1; evt.label=this.label; evt.scorelist=star.starscorelist; this.dispatchevent(evt); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。