JavaScript制作淘宝星级评分效果的思路


Posted in Javascript onJune 23, 2020

小编也是刚开始学JavaScript,觉得淘宝评星效果很棒,于是产生了自己写一个的想法,先给大家分享一下实现效果:

JavaScript制作淘宝星级评分效果的思路

现附上自己写的源代码

<!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>

一开始的时候用了两个for循环就是这样的:

for(var i=0;i<=n;i++)
  {
  document.getElementById("fiveStar").innerText="★";
  }
  for( var j=4;j>n;j--)
  {
  document.getElementById("fiveStar").innerText="☆";
  }

大神们估计已经看出来了,在for循环之后HTML里的span已经失去了作用,也就是说它只能评价一次.....
于是顺着这个思路想到了用数组解决这个问题,就是让评星效果里的每一颗星储存到数组里,写出了上述的代码,可楼主还犯了一个小错误,着实困恼了许久....
array[0]=document.getElementById("oneStar").innerText;
通过这样定义的数组....结果可想而知,后面的代码根本无法改变评星,后来意识到,这样的定义直接将ID为onestar的元素的内容赋值给了数组,也就是说数组成了一个指向数组的指针....自然无法改变对应元素的值.后来总算明白了....
之后又加了一些CSS效果
成品是这样的:

<!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(//img.jbzj.com/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>

以上就是JavaScript制作淘宝星级评分效果的思路,语言很直白,易理解,希望对大家的学习有所帮助,和小编一起去探索javascript更多的神奇之处,共同进步。

Javascript 相关文章推荐
JavaScript去除空格的几种方法
Oct 03 Javascript
js 中{},[]中括号,大括号使用详解
May 12 Javascript
真正的JQuery.ajax传递中文参数的解决方法
May 28 Javascript
基于JavaScript实现继承机制之构造函数方法对象冒充的使用详解
May 07 Javascript
node.js入门教程迷你书、node.js入门web应用开发完全示例
Apr 06 Javascript
js兼容pc端浏览器并有多种弹出小提示的手机端浮层控件实例
Apr 29 Javascript
jquery实现点击其他区域时隐藏下拉div和遮罩层的方法
Dec 23 Javascript
AngularJS基础 ng-non-bindable 指令详细介绍
Aug 02 Javascript
Angular路由简单学习
Dec 26 Javascript
js实现带三角符的手风琴效果
Mar 01 Javascript
vue v-for 点击当前行,获取当前行数据及event当前事件对象的操作
Sep 10 Javascript
浅谈vue2的$refs在vue3组合式API中的替代方法
Apr 18 Vue.js
jquery动态增加删减表格行特效
Nov 20 #Javascript
跟我学习javascript的异步脚本加载
Nov 20 #Javascript
JavaScript获取各大浏览器信息图示
Nov 20 #Javascript
跟我学习javascript创建对象(类)的8种方法
Nov 20 #Javascript
跟我学习javascript的最新标准ES6
Nov 20 #Javascript
详解JavaScript语言的基本语法要求
Nov 20 #Javascript
每天一篇javascript学习小结(面向对象编程)
Nov 20 #Javascript
You might like
PHP获取QQ达人QQ信息的方法
2015/03/05 PHP
php实现微信扫码支付
2017/03/26 PHP
php实现的生成迷宫与迷宫寻址算法完整实例
2017/11/06 PHP
javascript instanceof,typeof的区别
2010/03/24 Javascript
js锁屏解屏通过对$.ajax进行封装实现
2014/07/31 Javascript
jQuery实现鼠标划过展示大图的方法
2015/03/09 Javascript
简介JavaScript中substring()方法的使用
2015/06/06 Javascript
js实现文字滚动效果
2016/03/03 Javascript
javascript时间戳和日期字符串相互转换代码(超简单)
2016/06/22 Javascript
JavaScript 基础表单验证示例(纯Js实现)
2017/07/20 Javascript
vue中实现滚动加载更多的示例
2017/11/08 Javascript
vue和webpack项目构建过程常用的npm命令详解
2018/06/15 Javascript
js 计算月/周的第一天和最后一天代码
2020/02/01 Javascript
小程序卡片切换效果组件wxCardSwiper的实现
2020/02/13 Javascript
Python ljust rjust center输出
2008/09/06 Python
Python文件夹与文件的操作实现代码
2014/07/13 Python
python进阶教程之文本文件的读取和写入
2014/08/29 Python
python操作mysql中文显示乱码的解决方法
2014/10/11 Python
python email smtplib模块发送邮件代码实例
2018/04/26 Python
python爬虫 线程池创建并获取文件代码实例
2019/09/28 Python
Python函数式编程实例详解
2020/01/17 Python
python super函数使用方法详解
2020/02/14 Python
python中的对数log函数表示及用法
2020/12/09 Python
HTML5表单验证特性(知识点小结)
2020/03/10 HTML / CSS
国际知名设计师时装商店:Coggles
2016/09/05 全球购物
ktv收银员岗位职责
2013/12/16 职场文书
自主实习接收函
2014/01/13 职场文书
大学毕业感言50字
2014/02/07 职场文书
大学生个人实习的自我评价
2014/02/15 职场文书
彩妆大赛策划方案
2014/05/13 职场文书
MySQL 如何分析查询性能
2021/05/12 MySQL
MySQL中日期型单行函数代码详解
2021/06/21 MySQL
python opencv将多个图放在一个窗口的实例详解
2022/02/28 Python
win11无法添加打印机怎么办? 提示windows无法打开添加打印机的解决办法
2022/04/05 数码科技
Python探索生命起源 matplotlib细胞自动机动画演示
2022/04/21 Python
MySQL范围查询优化的场景实例详解
2022/06/10 MySQL