javascript實(shí)現(xiàn)評(píng)分功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)評(píng)分功能的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)的效果如下:

具體代碼如下:
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>評(píng)分</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">取消評(píng)分</a><a class="blueBtn sureStar">確認(rèn)</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 () {
//評(píng)分
var starRating = 0;
//鼠標(biāo)移入
$('.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) + '分');
});
//鼠標(biāo)離開(kāi)
$('.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) + '分')
}),
//點(diǎn)擊
$('.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('評(píng)分:' + (starRating.toFixed(1) + '分'))
});
//取消評(píng)分
$('.cancleStar').on('click',function () {
starRating = 0;
$('.photo span').find('.high').css('z-index',0);
$('.starNum').html(starRating.toFixed(1)+'分');
});
//確定評(píng)分
$('.sureStar').on('click',function () {
if(starRating===0) {
alert('最低一顆星!');
} else {
alert('評(píng)分:'+(starRating.toFixed(1)+'分'))
}
})
})
圖片stat.png和stat1.png分別如下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)星星評(píng)分功能實(shí)例代碼(兩種方法)
- jquery插件star-rating.js實(shí)現(xiàn)星級(jí)評(píng)分特效
- js點(diǎn)亮星星評(píng)分并獲取參數(shù)的js代碼
- js星星評(píng)分效果
- JS實(shí)現(xiàn)帶提示的星級(jí)評(píng)分效果完整實(shí)例
- javascript實(shí)現(xiàn)五星評(píng)分功能
- javascript 星級(jí)評(píng)分效果(手寫(xiě))
- js實(shí)現(xiàn)的星星評(píng)分功能函數(shù)
- AngularJS實(shí)現(xiàn)星星等級(jí)評(píng)分功能
- JavaScript制作淘寶星級(jí)評(píng)分效果的思路
相關(guān)文章
使用layui實(shí)現(xiàn)的左側(cè)菜單欄以及動(dòng)態(tài)操作tab項(xiàng)方法
今天小編就為大家分享一篇使用layui實(shí)現(xiàn)的左側(cè)菜單欄以及動(dòng)態(tài)操作tab項(xiàng)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
js showModalDialog 彈出對(duì)話(huà)框的簡(jiǎn)單實(shí)例(子窗體)
本篇文章主要是對(duì)js_showModalDialog彈出對(duì)話(huà)框的簡(jiǎn)單實(shí)例(子窗體) 進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
使用微信小程序制作核酸檢測(cè)點(diǎn)查詢(xún)工具
出門(mén)在外,沒(méi)有核酸證明寸步難行,此文將教你如何通過(guò)小程序制作一個(gè)工具幫你在人生地不熟的情況如何迅速找到核酸檢測(cè)點(diǎn),實(shí)現(xiàn)核酸點(diǎn)查詢(xún)、地圖導(dǎo)航、撥號(hào)等功能,需要的朋友可以參考下2022-10-10
淺談MUI框架中加載外部網(wǎng)頁(yè)或服務(wù)器數(shù)據(jù)的方法
下面小編就為大家分享一篇淺談MUI框架中加載外部網(wǎng)頁(yè)或服務(wù)器數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
javascript解析json格式的數(shù)據(jù)方法詳解
這篇文章主要介紹了javascript解析json格式的數(shù)據(jù)方法詳解,文章通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
es6函數(shù)之尾調(diào)用優(yōu)化實(shí)例分析
這篇文章主要介紹了es6函數(shù)之尾調(diào)用優(yōu)化,結(jié)合實(shí)例形式分析了es6函數(shù)尾調(diào)用優(yōu)化相關(guān)功能、原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
JavaScript代碼應(yīng)該放在HTML代碼哪個(gè)位置比較好?
這篇文章主要介紹了JavaScript代碼應(yīng)該放在HTML代碼哪個(gè)位置比較好?本文總結(jié)了多種放置JS代碼的方法,需要的朋友可以參考下2014-10-10

