经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jQuery实现滑动星星评分效果(每日分享)
来源:jb51  时间:2019/11/13 12:02:32  对本文有异议

每日分享效果,今天分享一个jQuery滑动星星评分效果。

jQuery星星评分制作5颗星星鼠标滑过评分打分效果,可取消评分结果,重新打分。

HTML代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <link rel="stylesheet" href="css/css.css" rel="external nofollow" >
  7. <script src="js/jquery.js"></script>
  8. </head>
  9. <body>
  10. <div id="starRating">
  11. <p class="photo">
  12. <span><i class="high"></i><i class="nohigh"></i></span>
  13. <span><i class="high"></i><i class="nohigh"></i></span>
  14. <span><i class="high"></i><i class="nohigh"></i></span>
  15. <span><i class="high"></i><i class="nohigh"></i></span>
  16. <span><i class="high"></i><i class="nohigh"></i></span>
  17. </p>
  18. <p class="starNum">0.0分</p>
  19. <div class="bottoms">
  20. <a class="garyBtn cancleStar">取消评分</a><a class="blueBtn sureStar">确认</a>
  21. </div>
  22. </div>
  23. <script>
  24. $(function () {
  25. //评分
  26. var starRating = 0;
  27. $('.photo span').on('mouseenter',function () {
  28. var index = $(this).index()+1;
  29. $(this).prevAll().find('.high').css('z-index',1)
  30. $(this).find('.high').css('z-index',1)
  31. $(this).nextAll().find('.high').css('z-index',0)
  32. $('.starNum').html((index*2).toFixed(1)+'分')
  33. })
  34. $('.photo').on('mouseleave',function () {
  35. $(this).find('.high').css('z-index',0)
  36. var count = starRating / 2
  37. if(count == 5) {
  38. $('.photo span').find('.high').css('z-index',1);
  39. } else {
  40. $('.photo span').eq(count).prevAll().find('.high').css('z-index',1);
  41. }
  42. $('.starNum').html(starRating.toFixed(1)+'分')
  43. })
  44. $('.photo span').on('click',function () {
  45. var index = $(this).index()+1;
  46. $(this).prevAll().find('.high').css('z-index',1)
  47. $(this).find('.high').css('z-index',1)
  48. starRating = index*2;
  49. $('.starNum').html(starRating.toFixed(1)+'分');
  50. alert('评分:'+(starRating.toFixed(1)+'分'))
  51. })
  52. //取消评分
  53. $('.cancleStar').on('click',function () {
  54. starRating = 0;
  55. $('.photo span').find('.high').css('z-index',0);
  56. $('.starNum').html(starRating.toFixed(1)+'分');
  57. })
  58. //确定评分
  59. $('.sureStar').on('click',function () {
  60. if(starRating===0) {
  61. alert('最低一颗星!');
  62. } else {
  63. alert('评分:'+(starRating.toFixed(1)+'分'))
  64. }
  65. })
  66. })
  67. </script>
  68. </body>
  69. </html>

CSS代码:

  1. #starRating .photo span {
  2. position: relative;
  3. display: inline-block;
  4. width: 44px;
  5. height: 42px;
  6. overflow: hidden;
  7. margin-right: 23px;
  8. cursor: pointer;
  9. }
  10. #starRating .photo span:last-child {
  11. margin-right: 0px;
  12. }
  13. #starRating .photo span .nohigh {
  14. position: absolute;
  15. width: 44px;
  16. height: 42px;
  17. top: 0;
  18. left: 0;
  19. background: url("../img/star.png");
  20. }
  21. #starRating .photo span .high {
  22. position: absolute;
  23. width: 44px;
  24. height: 42px;
  25. top: 0;
  26. left: 0;
  27. background: url("../img/star1.png");
  28. }
  29. #starRating .starNum {
  30. font-size: 26px;
  31. color: #de4414;
  32. margin-top: 4px;
  33. margin-bottom: 10px;
  34. }
  35. #starRating .bottoms {
  36. height: 54px;
  37. border-top: 1px solid #d8d8d8;
  38. }
  39. #starRating .photo {
  40. margin-top: 30px;
  41. }
  42. #starRating .bottoms a {
  43. margin-bottom: 0;
  44. }
  45. #starRating .bottoms .garyBtn {
  46. margin-right: 57px!important;
  47. }
  48. #starRating .bottoms a {
  49. width: 130px;
  50. height: 35px;
  51. line-height: 35px;
  52. border-radius: 3px;
  53. display: inline-block;
  54. font-size: 16px;
  55. transition: all 0.2s linear;
  56. margin: 16px 0 22px;
  57. text-align: center;
  58. cursor: pointer;
  59. }
  60. .garyBtn {
  61. margin-right: 60px!important;
  62. background-color: #e1e1e1;
  63. color: #999999;
  64. }
  65. .blueBtn {
  66. background-color: #1968b1;
  67. color: #fff;
  68. }
  69. .blueBtn:hover {
  70. background: #0e73d0;
  71. }

总结

以上所述是小编给大家介绍的jQuery实现滑动星星评分效果(每日分享),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对w3xue网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号