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 相关文章推荐
[原创]图片分页查看
Aug 28 Javascript
jquery获取tr中控件值并操作tr实现思路
Mar 27 Javascript
js实现Select列表内容自动滚动效果代码
Aug 20 Javascript
jQuery实现Flash效果上下翻动的中英文导航菜单代码
Sep 22 Javascript
简单实现JS对dom操作封装
Dec 02 Javascript
jQuery之简单的表单验证实例
Jul 07 Javascript
Bootstrap源码解读标签、徽章、缩略图和警示框(8)
Dec 26 Javascript
jQuery学习之DOM节点的插入方法总结
Jan 22 Javascript
js获取元素的偏移量offset简单方法(必看)
Jul 05 Javascript
JS运动特效之任意值添加运动的方法分析
Jan 24 Javascript
Angular路由ui-router配置详解
Aug 01 Javascript
vue-video-player视频播放器使用配置详解
Oct 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
php 图片上传类代码
2009/07/17 PHP
php操作MongoDB基础教程(连接、新增、修改、删除、查询)
2014/03/25 PHP
PHP实现的英文名字全拼随机排号脚本
2014/07/04 PHP
thinkPHP+mysql+ajax实现的仿百度一下即时搜索效果详解
2019/07/15 PHP
解决FLASH需要点击激活的代码
2006/12/20 Javascript
jQuery JSON实现无刷新三级联动实例探讨
2013/05/28 Javascript
javascript显示用户停留时间的简单实例
2013/08/05 Javascript
jQuery事件用法实例汇总
2014/08/29 Javascript
JQuery插入DOM节点的方法
2015/06/11 Javascript
SWFUpload多文件上传及文件个数限制的方法
2016/05/31 Javascript
jsp 自动编译机制详细介绍
2016/12/01 Javascript
JavaScript中的 attribute 和 jQuery中的 attr 方法浅析
2017/01/04 Javascript
jquery与ajax获取特殊字符实例详解
2017/01/08 Javascript
react.js 翻页插件实例代码
2017/01/19 Javascript
Bootstrap缩略图的创建方法
2017/03/22 Javascript
Angular实现较为复杂的表格过滤,删除功能示例
2017/12/23 Javascript
vue 2.x 中axios 封装的get 和post方法
2018/02/28 Javascript
three.js搭建室内场景教程
2018/12/30 Javascript
Vue分页器实现原理详解
2019/06/28 Javascript
VUE.js实现动态设置输入框disabled属性
2019/10/28 Javascript
小程序怎样让wx.navigateBack更好用的方法实现
2019/11/01 Javascript
JS实现百度搜索框关键字推荐
2020/02/17 Javascript
javascript单张多张图无缝滚动实例代码
2020/05/10 Javascript
[46:47]2014 DOTA2国际邀请赛中国区预选赛 DT VS HGT
2014/05/22 DOTA
Python中你应该知道的一些内置函数
2017/03/31 Python
Python的爬虫框架scrapy用21行代码写一个爬虫
2017/04/24 Python
Numpy 改变数组维度的几种方法小结
2018/08/02 Python
Python实现购物评论文本情感分析操作【基于中文文本挖掘库snownlp】
2018/08/07 Python
python面向对象入门教程之从代码复用开始(一)
2018/12/11 Python
Pycharm激活码激活两种快速方式(附最新激活码和插件)
2020/03/12 Python
解决django框架model中外键不落实到数据库问题
2020/05/20 Python
什么是.net
2015/08/03 面试题
小学校本培训方案
2014/06/06 职场文书
超市督导岗位职责
2015/04/10 职场文书
mysql批量新增和存储的方法实例
2021/04/07 MySQL
SQL Server 忘记密码以及重新添加新账号
2022/04/26 SQL Server