javascript实现评分功能


Posted in Javascript onJune 24, 2020

本文实例为大家分享了js实现评分功能的具体代码,供大家参考,具体内容如下

实现的效果如下:

javascript实现评分功能

具体代码如下:

html部分:

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <link rel="stylesheet" href="./index.css" >

 <title>评分</title>
</head>

<body>
 <div id="starRating">
  <p class="photo">
  <span><i class="high"></i><i class="nohigh"></i></span>
  <span><i class="high"></i><i class="nohigh"></i></span>
  <span><i class="high"></i><i class="nohigh"></i></span>
  <span><i class="high"></i><i class="nohigh"></i></span>
  <span><i class="high"></i><i class="nohigh"></i></span>
  </p>
  <p class="starNum">0.0分</p>
  <div class="bottoms">
  <a class="garyBtn cancleStar">取消评分</a><a class="blueBtn sureStar">确认</a>
 </div>

 </div>
 <script src="./jquery.js"></script>
 <script src="./index.js"></script>

</body>

</html>

CSS部分:

#starRating .photo span{
 position: relative;
 display: inline-block;
 width: 44px;
 height: 42px;
 overflow: hidden;
 margin-right: 23px;
 cursor: pointer;
 /* border: 1px solid black; */
}
#starRating .photo span:last-child{
 margin-right: 0px;
}
#starRating .photo span .nohigh{
 position: absolute;
 width: 44px;
 height: 42px;
 top: 0;
 left: 0;
 background: url(./star.png);
}
#starRating .photo span .high {
 position: absolute;
 width: 44px;
 height: 42px;
 top: 0;
 left: 0;
 background: url(./star1.png);
}
#starRating .starNum {
 font-size: 26px;
 color: #de4414;
 margin-top: 4px;
 margin-bottom: 10px;
}
#starRating .photo{
 margin-top: 30px;
}
#starRating .bottoms a {
 margin-bottom: 0;
}
#starRating .bottoms .garyBtn {
 margin-right: 57px!important;
}
#starRating .bottoms a {
 width: 130px;
 height: 35px;
 line-height: 35px;
 border-radius: 3px;
 display: inline-block;
 font-size: 16px;
 transition: all 0.2s linear;
 margin: 16px 0 22px;
 text-align: center;
 cursor: pointer;
}
.garyBtn {
 margin-right: 60px!important;
 background-color: #e1e1e1;
 color: #999999;
}
.blueBtn {
 background-color: #1968b1;
 color: #fff;
}
.blueBtn:hover {
 background: #0e73d0;
}

index.js

$(function () {
 //评分
 var starRating = 0;
 //鼠标移入
 $('.photo span').on('mouseenter', function () {
  var index = $(this).index() + 1;
  $(this).prevAll().find('.high').css('z-index', 1);
  $(this).find('.high').css('z-index', 1);
  $(this).nextAll().find('.high').css('z-index', 0);
  $('.starNum').html((index * 2).toFixed(1) + '分');
 });
 //鼠标离开
 $('.photo').on('mouseleave', function () {
  $(this).find('.high').css('z-index', 0);
  var count = starRating / 2;
  console.log(count);
  if (count == 5) {
   $('.photo span').find('.high').css('z-index', 1);
  } else {
   $('.photo span').eq(count).prevAll().find('.high').css('z-index', 1);
  }
  $('.starNum').html(starRating.toFixed(1) + '分')
 }),
  //点击
  $('.photo span').on('click', function () {
   var index = $(this).index() + 1;
   $(this).prevAll().find('.high').css('z-index', 1)
   $(this).find('.high').css('z-index', 1);
   starRating = index * 2;
   $('.starNum').html(starRating.toFixed(1) + '分');
   //alert('评分:' + (starRating.toFixed(1) + '分'))
  });
  //取消评分
  $('.cancleStar').on('click',function () {
   starRating = 0;
   $('.photo span').find('.high').css('z-index',0);
   $('.starNum').html(starRating.toFixed(1)+'分');
  });
  //确定评分
  $('.sureStar').on('click',function () {
   if(starRating===0) {
    alert('最低一颗星!');
   } else {
    alert('评分:'+(starRating.toFixed(1)+'分'))
   }
  })
})

图片stat.png和stat1.png分别如下

javascript实现评分功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Javascript 实用小技巧
Apr 07 Javascript
JSONP 跨域共享信息
Aug 16 Javascript
js猜数字小游戏的简单实现代码
Jul 02 Javascript
javascript截图 jQuery插件imgAreaSelect使用详解
May 04 Javascript
深入浅析JS的数组遍历方法(推荐)
Jun 15 Javascript
jQuery实现右下角可缩放大小的层完整实例
Jun 20 Javascript
基于Vue单文件组件详解
Sep 15 Javascript
vue2.0 兄弟组件(平级)通讯的实现代码
Jan 15 Javascript
angular 组件通信的几种实现方式
Jul 13 Javascript
详解a标签添加onclick事件的几种方式
Mar 29 Javascript
vue 实现一个简单的全局调用弹窗案例
Sep 10 Javascript
vue3使用vue-router的完整步骤记录
Jun 20 Vue.js
javascript实现移动端红包雨页面
Jun 23 #Javascript
JS实现canvas简单小画板功能
Jun 23 #Javascript
javascript+Canvas实现画板功能
Jun 23 #Javascript
vue.js实现照片放大功能
Jun 23 #Javascript
vue实现点击按钮切换背景颜色的示例代码
Jun 23 #Javascript
vue.js实现双击放大预览功能
Jun 23 #Javascript
vue实现分页的三种效果
Jun 23 #Javascript
You might like
php计算整个目录大小的方法
2015/06/01 PHP
CentOS下PHP7的编译安装及MySQL的支持和一些常见问题的解决办法
2015/12/17 PHP
PHP递归实现快速排序的方法示例
2017/12/18 PHP
PHP生成随机码的思路与方法实例探索
2019/04/11 PHP
js判断输入是否为正整数、浮点数等数字的函数代码
2010/11/17 Javascript
删除select中所有option选项jquery代码
2013/08/12 Javascript
node.js中的querystring.stringify方法使用说明
2014/12/10 Javascript
JS实现简单路由器功能的方法
2015/05/27 Javascript
js判断输入字符串是否为空、空格、null的方法总结
2016/06/14 Javascript
javascript匀速动画和缓冲动画详解
2016/10/20 Javascript
利用python分析access日志的方法
2016/10/26 Javascript
理解javascript中的Function.prototype.bind的方法
2017/02/03 Javascript
Angular模版驱动表单的使用总结
2018/05/05 Javascript
Vue一个案例引发的递归组件的使用详解
2018/11/15 Javascript
基于Vue2-Calendar改进的日历组件(含中文使用说明)
2019/04/14 Javascript
es6中Promise 对象基本功能与用法实例分析
2020/02/23 Javascript
vue 导航守卫和axios拦截器有哪些区别
2020/12/19 Vue.js
Python中实现两个字典(dict)合并的方法
2014/09/23 Python
Python3解决棋盘覆盖问题的方法示例
2017/12/07 Python
python+matplotlib绘制饼图散点图实例代码
2018/01/20 Python
padas 生成excel 增加sheet表的实例
2018/12/11 Python
Python调用graphviz绘制结构化图形网络示例
2019/11/22 Python
Django model.py表单设置默认值允许为空的操作
2020/05/19 Python
将tf.batch_matmul替换成tf.matmul的实现
2020/06/18 Python
美国汽车轮胎和轮毂销售网站:Tire Rack
2018/01/11 全球购物
英国探险旅游专家:Explore
2018/12/20 全球购物
咖啡馆创业计划书
2014/01/26 职场文书
个人简历自我评价范文
2014/02/04 职场文书
单位授权委托书范文
2014/08/02 职场文书
优秀中职教师事迹材料
2014/08/26 职场文书
致800米运动员广播稿(10篇)
2014/10/17 职场文书
优秀共产党员推荐材料
2014/12/18 职场文书
2015年中职班主任工作总结
2015/05/25 职场文书
城南旧事读书笔记
2015/06/29 职场文书
扩展多台相同的Web服务器
2021/04/01 Servers
使用compose函数优化代码提高可读性及扩展性
2022/06/16 Javascript