js實(shí)現(xiàn)星星打分效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)星星打分的具體代碼,供大家參考,具體內(nèi)容如下
最終效果如下
html部分
<div class='container'>小主的評(píng)價(jià):</div> <span></span>
css樣式
<style> .star { font-size: 20px; color: gold; cursor: pointer; } .container { display: inline-block; text-align: center; }</style>
js部分
<script src='https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js'></script> <script> $(function () { let count = 5 let score = 3 for (let i = 0; i < count; i++) {let star = $('<i/>').addClass('iconfont').addClass('star')if (i < score) star.addClass('icon-xingxing')else star.addClass('icon-xingxing1')$('.container').append(star) } $('.star').mouseenter(function () {let index = $(this).index()$('.star').each(function (i) { if (i <= index) $(this).addClass('icon-xingxing').removeClass('icon-xingxing1') else $(this).addClass('icon-xingxing1').removeClass('icon-xingxing')}) }) $('.star').mouseleave(function () {$('.star').each(function (i) { if (i < score) $(this).addClass('icon-xingxing').removeClass('icon-xingxing1') else $(this).addClass('icon-xingxing1').removeClass('icon-xingxing')}) }) $('.star').click(function () {score = $(this).index() + 1$('span').html(`${score}分`) }) $('span').html(`${score}分`) })</script>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 如何在 Matplotlib 中繪制垂直線2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. ASP常用日期格式化函數(shù) FormatDate()4. python中@contextmanager實(shí)例用法5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 如何通過python實(shí)現(xiàn)IOU計(jì)算代碼實(shí)例9. 開發(fā)效率翻倍的Web API使用技巧10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
