javascript实现评分功能

时间:2021-05-26

本文实例为大家分享了js实现评分功能的具体代码,供大家参考,具体内容如下

实现的效果如下:

具体代码如下:

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; }#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分别如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章