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 相关文章推荐
JS 事件绑定函数代码
Apr 28 Javascript
JS 跳转页面延迟2种方法
Mar 29 Javascript
jquery遍历筛选数组的几种方法和遍历解析json对象
Dec 13 Javascript
基于JS如何实现给字符加千分符(65,541,694,158)
Aug 03 Javascript
基于JavaScript实现在新的tab页打开url
Aug 04 Javascript
javaScript基础详解
Jan 19 Javascript
JS Select下拉框(支持输入模糊查询)
Feb 04 Javascript
jquery实现tab键进行选择后enter键触发click行为
Mar 29 jQuery
详解Node.js 命令行程序开发教程
Jun 07 Javascript
五步轻松实现zTree的使用
Nov 01 Javascript
element-ui 实现响应式导航栏的示例代码
May 08 Javascript
详解微信小程序入门从这里出发(登录注册、开发工具、文件及结构介绍)
Jul 21 Javascript
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从数组中随机抽取一些元素的代码
2012/11/05 PHP
PHP图片加水印实现方法
2016/05/06 PHP
Laravel监听数据库访问,打印SQL的例子
2019/10/24 PHP
Whatever:hover 无需javascript让IE支持丰富伪类
2010/06/29 Javascript
JS动态添加option和删除option(附实例代码)
2013/04/01 Javascript
node.js不得不说的12点内容
2014/07/14 Javascript
[原创]推荐10款最热门jQuery UI框架
2014/08/19 Javascript
iframe里面的元素触发父窗口元素事件的jquery代码
2014/10/19 Javascript
jQuery实现简单的点赞效果
2020/05/29 Javascript
ionic在开发ios系统微信时键盘挡住输入框的解决方法(键盘弹出问题)
2016/09/06 Javascript
node.js实现博客小爬虫的实例代码
2016/10/08 Javascript
详解浏览器渲染页面过程
2017/02/09 Javascript
基于nodejs res.end和res.send的区别
2018/05/14 NodeJs
微信小程序自定义底部弹出框
2020/11/16 Javascript
javascript将非数值转换为数值
2018/09/13 Javascript
纯javascript前端实现base64图片下载(兼容IE10+)
2018/09/14 Javascript
详解vue高级特性
2020/06/09 Javascript
利用JavaScript为句子加标题的3种方法示例
2021/01/05 Javascript
在Python中使用cookielib和urllib2配合PyQuery抓取网页信息
2015/04/25 Python
Python 爬虫多线程详解及实例代码
2016/10/08 Python
Python基于更相减损术实现求解最大公约数的方法
2018/04/04 Python
Python实现多态、协议和鸭子类型的代码详解
2019/05/05 Python
python3安装crypto出错及解决方法
2019/07/30 Python
使用Python串口实时显示数据并绘图的例子
2019/12/26 Python
python+OpenCV实现图像拼接
2020/03/05 Python
Django中文件上传和文件访问微项目的方法
2020/04/27 Python
使用豆瓣源来安装python中的第三方库方法
2021/01/26 Python
adidas官方旗舰店:德国运动用品制造商
2017/11/25 全球购物
买卖正宗运动鞋:GOAT
2019/12/06 全球购物
仓管员岗位职责范文
2013/11/08 职场文书
《青海高原一株柳》教学反思
2014/04/25 职场文书
《搭石》教学反思
2016/02/18 职场文书
创业计划书之密室逃脱
2019/11/08 职场文书
南阳市白酒市场的调查报告
2019/11/08 职场文书
Python代码风格与编程习惯重要吗?
2021/06/03 Python
OpenCV绘制圆端矩形的示例代码
2021/08/30 Python