您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關JS如何實現五星好評效果的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
用JS實現面向對象方法實現京東的五星好評效果。
鼠標滑過時的顯示:
當評價完成后,關閉瀏覽器重新打開頁面,還是上次的評價結果。用cookie實現。
具體實現如下:
import Componenet from "./Component.js"; export default class Stars extends Componenet { label; STARS_NUM = 5; starArr = []; score = 0; starsCon; faceIcon; scoreCon; index = -1; name; static STARS_LIST={}; //存儲當前頁面中所有的五星評價結果,一個商品為一組。用商品的id作為key,用商品評價組成一個數組,作為value。 date=new Date(); constructor(_label,_name) { super("div"); this.name=_name; this.label = _label; Object.assign(this.elem.style, { width:"200px", height: "16px", float: "left", marginRight: "20px", marginBottom: "10px", fontSize: "12px", color: "#666", lineHeight: "16px", userSelect: "none", position: "relative", top: "20px", left: "20px", }) // 解析cookie中存儲的評論結果并在創建每個評論時初始化score的值。 this.initScore(); // 創建評價標簽部分 this.createLabel(); // 創建星星部分 this.createStars(); // 創建分數部分 this.createScore(); // 初始化星星樣式。 this.changeStarStyle(this.score-1); // 初始化分數 this.changeScore(this.score); // 添加鼠標滑過點擊事件。 this.starsCon.addEventListener("mouseenter", e => this.mouseHandler(e)); this.starsCon.addEventListener("mouseleave", e => this.mouseHandler(e)); this.starsCon.addEventListener("mouseover", e => this.mouseHandler(e)); this.starsCon.addEventListener("click", e => this.clickHandler(e)); this.date.setFullYear(2021); } appendTo(_parent){ super.appendTo(_parent); if(!Stars.STARS_LIST[this.name]) Stars.STARS_LIST[this.name]=[]; Stars.STARS_LIST[this.name].push(this.label+"="+this.score); } clickHandler(e){ if(e.target.constructor!==HTMLLIElement) return this.index = this.starArr.indexOf(e.target); this.score = this.index+1; this.changeStarStyle(this.index); this.changeScore(this.index+1); // 每次點擊都將評論的結果存儲到cookie中,以便下次打開頁面時讀取。STARS_LIST中存儲的是label作為key,score作為value的數據。 this.storageScore(); } storageScore(){ for(let prop in Stars.STARS_LIST){ if(prop === this.name){ Stars.STARS_LIST[prop].forEach((item,index)=>{ if(item.includes(this.label)) Stars.STARS_LIST[prop][index] = this.label+"="+this.score; }); } } document.cookie="comment="+ JSON.stringify(Stars.STARS_LIST)+";expires="+this.date.toUTCString(); } mouseHandler(e) { switch (e.type) { case "mouseenter": this.faceIcon.style.display = "block"; break; case "mouseleave": this.faceIcon.style.display = "none"; this.changeStarStyle(this.index); this.changeScore(this.score); break; case "mouseover": let index = this.starArr.indexOf(e.target); this.changeStarStyle(index); this.changeScore(index + 1); this.changeFaceStyle(index); break; } } changeStarStyle(_i) { for (let n = 0; n < this.starArr.length; n++) { if (n <= _i || n < this.score) { this.starArr[n].style.backgroundPositionY = "-16px"; } else { this.starArr[n].style.backgroundPositionY = "0px"; } } } changeFaceStyle(_i) { this.faceIcon.style.left = _i * 16 + "px"; this.faceIcon.style.backgroundPositionX = (_i + 1) * 20 + "px"; } changeScore(_i) { this.scoreCon.textContent = _i + "分"; } createLabel() { let label = document.createElement("span"); Object.assign(label.style, { float: "left", padding: "0 5px", }) label.textContent = this.label; this.elem.appendChild(label); } createStars() { this.starsCon = document.createElement("ul"); Object.assign(this.starsCon.style, { margin: 0, padding: 0, listStyle: "none", width: "80px", height: "16px", float: "left", position: "relative", }) for (let i = 0; i < this.STARS_NUM; i++) { let li = document.createElement("li"); Object.assign(li.style, { width: "16px", height: "16px", float: "left", backgroundImage: "url(./star_img/commstar.png)", }) this.starArr.push(li); this.starsCon.appendChild(li); } this.faceIcon = document.createElement("div"); Object.assign(this.faceIcon.style, { width: "16px", height: "16px", backgroundImage: "url(./star_img/face-red.png)", backgroundPositionX: "-80px", position: "absolute", left: "0", top: "-16px", display: "none", }) this.starsCon.appendChild(this.faceIcon); this.elem.appendChild(this.starsCon); } createScore() { this.scoreCon = document.createElement("div"); Object.assign(this.scoreCon.style, { height: "16px", width:"20px", float: "left", padding: "0 5px", }) this.scoreCon.textContent = this.score + "分", this.elem.appendChild(this.scoreCon); } initScore(){ // 直接讀取cookie顯示如下 // comment={"1001":["商品符合度=5","店家服務態度=0","快遞配送速度=0","快遞員服務=0","快遞包裝=0"],"1002":["商品符合度=0","店家服務態度=0","快遞配送速度=0","快遞員服務=0","快遞包裝=0"]} // 解析cookie中存儲的評論結果。 if(!document.cookie.includes("comment=")) return let o = JSON.parse(document.cookie.match(/(\{.*?\})/)[1]); if(!o) return // 解析后的o如下 // ["商品符合度=1", "店家服務態度=0", "快遞配送速度=0", "快遞員服務=0", "快遞包裝=0"] for(let prop in o){ if(this.name===prop){ this.score=o[prop].reduce((value,item,index)=>{ let arr=item.split("="); if(arr[0].includes(this.label)) value=parseInt(arr[1]); return value; },0) } } } }
使用時傳入標簽,新建實例。
import Stars from './js/Stars.js'; let list=["商品符合度","店家服務態度","快遞配送速度","快遞員服務","快遞包裝"]; // let star = new Stars(list[0]); // star.appendTo("body"); list.forEach(item=>{ // 傳入標簽和對應的商品id let star = new Stars(item,"1001"); star.appendTo(".div1"); }) list.forEach(item=>{ let star = new Stars(item,"1002"); star.appendTo(".div2"); })
感謝各位的閱讀!關于“JS如何實現五星好評效果”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。