Use JS to implement object-oriented methods to achieve JD.com's five-star review effect. Display when the mouse moves over: When the evaluation is completed, close the browser and reopen the page, and the last evaluation result will still be displayed. This is done using cookies. The specific implementation is as follows: 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={}; //Store all five-star review results on the current page, one product per group. Use the product ID as the key and the product reviews to form an array as the 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", }) // Parse the review results stored in the cookie and initialize the score value when creating each review. this.initScore(); // Create the evaluation label part this.createLabel(); // Create the stars part this.createStars(); // Create the score part this.createScore(); // Initialize the star style. this.changeStarStyle(this.score-1); // Initialize score this.changeScore(this.score); // Add mouse rollover and click events. 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); // Each click will store the result of the comment in the cookie so that it can be read the next time the page is opened. STARS_LIST stores data with label as key and score as 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 + "points"; } 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(){ // Directly read the cookie to display as follows // comment={"1001":["Product Conformity=5","Shop Service Attitude=0","Express Delivery Speed=0","Courier Service=0","Express Packaging=0"],"1002":["Product Conformity=0","Shop Service Attitude=0","Express Delivery Speed=0","Courier Service=0","Express Packaging=0"]} // Parse the comment results stored in the cookie. if(!document.cookie.includes("comment=")) return let o = JSON.parse(document.cookie.match(/(\{.*?\})/)[1]); if(!o) return // The parsed o is as follows // ["Product Conformity=1", "Shop Service Attitude=0", "Express Delivery Speed=0", "Courier Service=0", "Express Packaging=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) } } } } When using, pass in the tag and create a new instance. import Stars from './js/Stars.js'; let list=["Product Conformity","Shopkeeper Service Attitude","Express Delivery Speed","Courier Service","Express Packaging"]; // let star = new Stars(list[0]); // star.appendTo("body"); list.forEach(item=>{ // Pass in the label and the corresponding product id let star = new Stars(item,"1001"); star.appendTo(".div1"); }) list.forEach(item=>{ let star = new Stars(item,"1002"); star.appendTo(".div2"); }) The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL Index Optimization Explained
>>: A brief analysis of the use of zero copy technology in Linux
This article shares with you the installation tut...
Table of contents JSX environment construction Se...
Table of contents 1. Where is the self-incremente...
Table of contents The first The second Native Js ...
Table of contents 1. Check the status of the serv...
Download and install JDK Step 1: First download t...
1. Set up HOST on the host Macbook The previous d...
Putting input and img on the same line, the img ta...
Today I found a problem in HTML. There are many d...
<br />When uploading on some websites, a [Se...
Install boost There are many ways to call C/C++ f...
Table of contents Method 1: Routing meta informat...
Problem Description When we are working on a proj...
Simple description Since it was built with Centos...
[LeetCode] 175.Combine Two Tables Table: Person +...