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资料toString 方法
Mar 13 Javascript
javascript 面向对象编程 聊聊对象的事
Sep 17 Javascript
一个JavaScript操作元素定位元素的实例
Oct 29 Javascript
JavaScript分秒倒计时器实现方法
Feb 02 Javascript
JavaScript 数组some()和filter()的用法及区别
May 20 Javascript
jQuery实现摸拟alert提示框
May 22 Javascript
javascript-解决mongoose数据查询的异步操作
Dec 22 Javascript
深入探究angular2 UI组件之primeNG用法
Jul 26 Javascript
原生js实现简单的模态框示例
Sep 08 Javascript
实时监控input框,实现输入框与下拉框联动的实例
Jan 23 Javascript
JavaScript设计模式之享元模式实例详解
Jan 17 Javascript
vue.js实现备忘录demo
Jun 26 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处理大量表单字段的便捷方法
2015/02/07 PHP
thinkPHP使用post方式查询时分页失效的解决方法
2015/12/09 PHP
laravel 查询数据库获取结果实现判断是否为空
2019/10/24 PHP
AutoSave/自动存储功能实现
2007/03/24 Javascript
Prototype Selector对象学习
2009/07/23 Javascript
jQuery事件绑定.on()简要概述及应用
2013/02/07 Javascript
深入理解JavaScript 闭包究竟是什么
2013/04/12 Javascript
javascrip关于继承的小例子
2013/05/10 Javascript
js函数排序的实例代码
2013/07/01 Javascript
一个简单的实现下拉框多选的插件可移植性比较好
2014/05/05 Javascript
javascript学习笔记之10个原生技巧
2014/05/21 Javascript
Windows8下搭建Node.js开发环境教程
2014/09/03 Javascript
用JavaScript实现PHP的urlencode与urldecode函数
2015/08/13 Javascript
JS使用onerror捕获异常示例
2016/08/03 Javascript
详解angular2封装material2对话框组件
2017/03/03 Javascript
jQuery中clone()函数实现表单中增加和减少输入项
2017/05/13 jQuery
Angular.js自动化测试之protractor详解
2017/07/07 Javascript
bootstrap-table组合表头的实现方法
2017/09/07 Javascript
浅谈Vuex@2.3.0 中的 state 支持函数申明
2017/11/22 Javascript
详解react-router 4.0 下服务器如何配合BrowserRouter
2017/12/29 Javascript
jquery应用实例分享_实现手风琴特效
2018/02/01 jQuery
Vue CLI3 如何支持less的方法示例
2018/08/29 Javascript
Vue resource三种请求格式和万能测试地址
2018/09/26 Javascript
微信小程序点击列表跳转到对应详情页过程解析
2019/09/26 Javascript
通过代码实例展示Python中列表生成式的用法
2015/03/31 Python
使用Python3编写抓取网页和只抓网页图片的脚本
2015/08/20 Python
python中requests爬去网页内容出现乱码问题解决方法介绍
2017/10/25 Python
实践Vim配置python开发环境
2018/07/02 Python
详解python解压压缩包的五种方法
2019/07/05 Python
Django--权限Permissions的例子
2019/08/28 Python
selenium中get_cookies()和add_cookie()的用法详解
2020/01/06 Python
python opencv圆、椭圆与任意多边形的绘制实例详解
2020/02/06 Python
如何设定的weblogic的热启动模式(开发模式)与产品发布模式
2012/09/08 面试题
电子商务系毕业生自荐信
2014/05/29 职场文书
项目经理任命书内容
2014/06/06 职场文书
sass 常用备忘案例详解
2021/09/15 HTML / CSS