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 相关文章推荐
HTML页面如何象ASP一样接受参数
Feb 07 Javascript
extJs 常用到的增,删,改,查操作代码
Dec 28 Javascript
JQuery 构建客户/服务分离的链接模型中Table分页代码效率初探
Jan 22 Javascript
精通JavaScript 纠正 cleanWhitespace函数
Mar 11 Javascript
JavaScript中的函数声明和函数表达式区别浅析
Mar 27 Javascript
JS实现很实用的对联广告代码(可自适应高度)
Sep 18 Javascript
js仿百度登录页实现拖动窗口效果
Mar 11 Javascript
AngularJS基于ngInfiniteScroll实现下拉滚动加载的方法
Dec 14 Javascript
JavaScript中document.referrer的用法详解
Jul 04 Javascript
原生js实现form表单序列化的方法
Aug 02 Javascript
小程序实现投票进度条
Nov 20 Javascript
微信小程序picker组件两列关联使用方式
Oct 27 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 flush类输出缓冲剖析
2008/10/19 PHP
理解php Hash函数,增强密码安全
2011/02/25 PHP
php计算两个日期时间差(返回年、月、日)
2014/06/19 PHP
ThinkPHP函数详解之M方法和R方法
2015/09/10 PHP
学习php设计模式 php实现访问者模式(Visitor)
2015/12/07 PHP
Symfony2学习笔记之插件格式分析
2016/03/17 PHP
php登录超时检测功能实例详解
2017/03/21 PHP
PHP设计模式之装饰器模式定义与用法详解
2018/04/02 PHP
PHP与以太坊交互详解
2018/08/24 PHP
php实现通过stomp协议连接ActiveMQ操作示例
2020/02/23 PHP
学习ExtJS Window常用方法
2009/10/07 Javascript
Javascript attachEvent传递参数的办法
2009/12/14 Javascript
EasyUI的treegrid组件动态加载数据问题的解决办法
2011/12/11 Javascript
JS实现清除指定cookies的方法
2014/09/20 Javascript
利用jQuery中的ajax分页实现代码
2016/02/25 Javascript
sublime text配置node.js调试(图文教程)
2017/11/23 Javascript
微信小程序实现打开内置地图功能【附源码下载】
2017/12/07 Javascript
解析vue路由异步组件和懒加载案例
2018/06/08 Javascript
vue+iview/elementUi实现城市多选
2019/03/28 Javascript
Vue项目中如何使用Axios封装http请求详解
2019/10/23 Javascript
小程序采集录音并上传到后台
2019/11/22 Javascript
小谈angular ng deploy的实现
2020/04/07 Javascript
在Python中使用pngquant压缩png图片的教程
2015/04/09 Python
python实现可以断点续传和并发的ftp程序
2016/09/13 Python
详解python之heapq模块及排序操作
2019/04/04 Python
Django实现文件上传和下载功能
2019/10/06 Python
Python 下载及安装详细步骤
2019/11/04 Python
Python爬虫库BeautifulSoup的介绍与简单使用实例
2020/01/25 Python
Django 5种类型Session使用方法解析
2020/04/29 Python
HTML5学习笔记之History API
2015/02/26 HTML / CSS
亚洲最大的运动鞋寄售店:KicksCrew
2020/11/26 全球购物
全陪导游词开场白
2015/05/29 职场文书
合同审查法律意见书
2015/06/04 职场文书
微信小程序和php的登录实现
2021/04/01 PHP
html中显示特殊符号(附带特殊字符对应表)
2021/06/21 HTML / CSS
Android Flutter实现3D动画效果示例详解
2022/04/07 Java/Android