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 相关文章推荐
jQuery插件-jRating评分插件源码分析及使用方法
Dec 28 Javascript
让jQuery Mobile不显示讨厌loading界面的方法
Feb 19 Javascript
jQuery实现的简洁下拉菜单导航效果代码
Aug 26 Javascript
基于jquery实现图片上传本地预览功能
Jan 08 Javascript
jQuery 3 中的新增功能汇总介绍
Jun 12 Javascript
浅谈js中对象的使用
Aug 11 Javascript
用瀑布流的方式在网页上插入图片的简单实现方法
Sep 23 Javascript
Jquery AJAX POST与GET之间的区别详细介绍
Oct 17 Javascript
JS查找英文文章中出现频率最高的单词
Mar 20 Javascript
vue实现商城上货组件简易版
Nov 27 Javascript
jquery实现左右轮播切换效果
Jan 01 jQuery
AngularJS ui-router刷新子页面路由的方法
Jul 23 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
eWebEditor v3.8 商业完整版 (PHP)
2006/12/06 PHP
简单的方法让你的后台登录更加安全(php中加session验证)
2012/08/22 PHP
你必须知道的JavaScript 变量命名规则详解
2013/05/07 Javascript
JavaScript使用HTML5的window.postMessage实现跨域通信例子
2014/04/11 Javascript
JavaScript实现彩虹文字效果的方法
2015/04/16 Javascript
jQuery实现文件上传进度条特效
2015/08/12 Javascript
详细谈谈javascript的对象
2016/07/31 Javascript
Nodejs中使用phantom将html转为pdf或图片格式的方法
2017/09/18 NodeJs
babel的使用及安装配置教程
2018/02/22 Javascript
使用vue2实现带地区编号和名称的省市县三级联动效果
2018/11/05 Javascript
简述vue-cli中chainWebpack的使用方法
2019/07/30 Javascript
微信小程序授权登陆及每次检查是否授权实例代码
2019/09/18 Javascript
为nuxt项目写一个面包屑cli工具实现自动生成页面与面包屑配置
2019/09/29 Javascript
详解Vue的ref特性的使用
2020/01/24 Javascript
vue移动端写的拖拽功能示例代码
2020/09/09 Javascript
[02:58]献给西雅图的情书_高清
2014/05/29 DOTA
python利用paramiko连接远程服务器执行命令的方法
2017/10/16 Python
Python面向对象之继承代码详解
2018/01/29 Python
python opencv3实现人脸识别(windows)
2018/05/25 Python
python实现AES和RSA加解密的方法
2019/03/28 Python
Python TCPServer 多线程多客户端通信的实现
2019/12/31 Python
Django实现将一个字典传到前端显示出来
2020/04/03 Python
HTML5混合开发二维码扫描以及调用本地摄像头
2017/12/27 HTML / CSS
西班牙最大的婴儿用品网上商店:Bebitus
2019/05/30 全球购物
美国滑板店:Tactics
2020/11/08 全球购物
世界上最大的艺术社区:SAA
2020/12/30 全球购物
一套中级Java程序员笔试题
2015/01/14 面试题
财务分析个人的自荐书范文
2013/11/24 职场文书
历史系自荐信范文
2013/12/24 职场文书
初一地理教学反思
2014/01/16 职场文书
后勤服务中心总经理工作职责
2014/03/03 职场文书
初三开学计划书
2014/04/27 职场文书
2014年工作总结与下年工作计划
2014/11/27 职场文书
七年级英语教学反思
2016/02/15 职场文书
python编程学习使用管道Pipe编写优化代码
2021/11/20 Python
nginx中封禁ip和允许内网ip访问的实现示例
2022/03/17 Servers