


Ideas for creating Taobao star rating effects with JavaScript_javascript skills
The editor also just started learning JavaScript, and felt that the Taobao star rating effect was great, so I came up with the idea of writing one myself. I would like to share with you the implementation effect first:
Attached is the source code I wrote
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script language="JavaScript" type="text/javascript"> function star(n) { var array=new Array(); array[0]=document.getElementById("oneStar"); array[1]=document.getElementById("twoStar"); array[2]=document.getElementById("threeStar"); array[3]=document.getElementById("fourStar"); array[4]=document.getElementById("fiveStar"); for(var i=0;i<=n;i++) { array[i].innerText="★"; } for( var j=4;j>n;j--) { array[j].innerText="☆"; } document.getElementById("evaluate").innerText="您的评价是"+(n+1)+"星"; } </script> <title>评星</title> </head> <body> <strong>请您对我们作出评价:</strong> <span id="star"> <span style="cursor: pointer " onclick="star(0)"id="oneStar" >☆</span> <span style="cursor: pointer " onclick="star(1)" id="twoStar" >☆</span> <span style="cursor: pointer " onclick="star(2)" id="threeStar" >☆</span> <span style="cursor: pointer " onclick="star(3)" id="fourStar" >☆</span> <span style="cursor: pointer " onclick="star(4)" id="fiveStar" >☆</span> </span><span id="evaluate"></span> </body> </html>
At the beginning, I used two for loops like this:
for(var i=0;i<=n;i++) { document.getElementById("fiveStar").innerText="★"; } for( var j=4;j>n;j--) { document.getElementById("fiveStar").innerText="☆"; }
Masters may have noticed that span in HTML has lost its function after the for loop, which means it can only be evaluated once...
So I followed this idea and thought of using an array to solve this problem, which is to store each star in the star rating effect in the array. I wrote the above code, but the poster also made a small mistake, which really bothered me for a long time. ....
array[0]=document.getElementById("oneStar").innerText;
Through the array defined in this way... the result can be imagined. The following code cannot change the rating at all. Later I realized that such a definition directly assigns the content of the element with ID onestar to the array, which means that the array consists of I got a pointer to the array.... Naturally, I can't change the value of the corresponding element. I finally figured it out....
Then I added some CSS effects
The finished product looks like this:
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>淘宝评分效果</title> <style type="text/css"> ul, li {margin: 0; padding: 0; border: 0;} .shop-rating { height: 25px; overflow: hidden; zoom: 1; padding: 2px 0; position: relative; z-index: 999; font:12px Arial; color:#000; line-height:1.2em } .shop-rating span { height: 23px; display: block; line-height: 23px; float: left; } .shop-rating span.title { width: 125px; text-align: right; margin-right: 5px; } .shop-rating ul { float: left; } .shop-rating .result { margin-left: 20px; padding-top: 2px; } .shop-rating .result span { color: #ff6d02; } .rating-level, .rating-level a { background: url(http://files.jb51.net/demoimg/201007/o_star.png) no-repeat scroll 1000px 1000px; } .rating-level { background-position: 0px 0px; width: 120px; height: 23px; position: relative; z-index: 1000; } .shop-rating .result em { color: #f60; font-family: arial; font-weight: bold; } .rating-level li { display: inline; } .rating-level a { line-height: 23px; height: 23px; position: absolute; top: 0px; left: 0px; text-indent: -999em; *zoom: 1; outline: none; } .rating-level a.one-star { width: 20%; z-index: 6; } .rating-level a.two-stars { width: 40%; z-index: 5; } .rating-level a.three-stars { width: 60%; z-index: 4; } .rating-level a.four-stars { width: 80%; z-index: 3; } .rating-level a.five-stars { width: 100%; z-index: 2; } .rating-level .current-rating, .rating-level a:hover {background-position:0 -28px} .rating-level a.one-star:hover,.rating-level a.two-stars:hover,.rating-level a.one-star.current-rating,.rating-level a.two-stars.current-rating{background-position:0 -116px;} .rating-level .three-stars .current-rating,.rating-level .four-stars .current-rating,.rating-level .five-stars .current-rating{background-position:0 -28px;} </style> </head> <body> <div class="shop-rating"> <span class="title">你对我人品的评价:</span> <ul class="rating-level" id="stars2"> <li><a href="javascript:void(0);" class="one-star" star:value="20">20</a></li> <li><a href="javascript:void(0);" class="two-stars" star:value="40">40</a></li> <li><a href="javascript:void(0);" class="three-stars" star:value="60">60</a></li> <li><a href="javascript:void(0);" class="four-stars" star:value="80">80</a></li> <li><a href="javascript:void(0);" class="five-stars" star:value="100">100</a></li> </ul> <span id="stars2-tips" class="result"></span> <input type="hidden" id="stars2-input" name="b" value="" size="2" /> </div> <script> var TB = function() { var T$ = function(id) { return document.getElementById(id) } var T$$ = function(r, t) { return (r || document).getElementsByTagName(t) } var Stars = function(cid, rid, hid, config) { var lis = T$$(T$(cid), 'li'), curA; for (var i = 0, len = lis.length; i < len; i++) { lis[i]._val = i; lis[i].onclick = function() { T$(rid).innerHTML = '<em>' + (T$(hid).value = T$$(this, 'a')[0].getAttribute('star:value')) + '分</em> - ' + config.info[this._val]; curA = T$$(T$(cid), 'a')[T$(hid).value / config.step - 1]; }; lis[i].onmouseout = function() { curA && (curA.className += config.curcss); } lis[i].onmouseover = function() { curA && (curA.className = curA.className.replace(config.curcss, '')); } } }; return {Stars: Stars} }().Stars('stars2', 'stars2-tips', 'stars2-input', { 'info' : ['人品极差', '人品不咋地', '人品一般吧', '人品不错', '人品极好啊'], 'curcss': ' current-rating', 'step': 20 }); </script> </body> </html>
The above is the idea of using JavaScript to create Taobao star rating effect. The language is very straightforward and easy to understand. I hope it will be helpful to everyone's learning. Let's explore more magic of JavaScript with the editor and make progress together.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
