javascript實(shí)現(xiàn)評分功能
本文實(shí)例為大家分享了js實(shí)現(xiàn)評分功能的具體代碼,供大家參考,具體內(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='http://www.4tl426be.cn/bcjs/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'>確認(rèn)</a> </div> </div> <script src='http://www.4tl426be.cn/bcjs/jquery.js'></script> <script src='http://www.4tl426be.cn/bcjs/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; //鼠標(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)離開 $(’.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(’評分:’ + (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分別如下
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. android 控件同時(shí)監(jiān)聽單擊和雙擊實(shí)例2. 解決vue頁面刷新,數(shù)據(jù)丟失的問題3. Python 忽略文件名編碼的方法4. vue路由分文件拆分管理詳解5. 詳解android adb常見用法6. vue+vuex+axios從后臺獲取數(shù)據(jù)存入vuex,組件之間共享數(shù)據(jù)操作7. python logging.info在終端沒輸出的解決8. android studio實(shí)現(xiàn)簡單的計(jì)算器(無bug)9. python Selenium 庫的使用技巧10. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作
