温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用jQuery怎么实现一个滑动星星评分效果

发布时间:2021-04-17 16:56:43 阅读:199 作者:Leah 栏目:web开发
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家介绍使用jQuery怎么实现一个滑动星星评分效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

HTML代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="css/css.css" rel="external nofollow" >
  <script src="js/jquery.js"></script>
</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>
  $(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
      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)+'分'))
      }
    })
  })
</script>
</body>
</html>

CSS代码:

#starRating .photo span {
  position: relative;
  display: inline-block;
  width44px;
  height42px;
  overflow: hidden;
  margin-right23px;
  cursor: pointer;
}
#starRating .photo span:last-child {
  margin-right0px;
}
#starRating .photo span .nohigh {
  position: absolute;
  width44px;
  height42px;
  top0;
  left0;
  backgroundurl("../img/star.png");
}
#starRating .photo span .high {
  position: absolute;
  width44px;
  height42px;
  top0;
  left0;
  backgroundurl("../img/star1.png");
}
#starRating .starNum {
  font-size26px;
  color#de4414;
  margin-top4px;
  margin-bottom10px;
}
#starRating .bottoms {
  height54px;
  border-top1px solid #d8d8d8;
}
#starRating .photo {
  margin-top30px;
}
#starRating .bottoms a {
  margin-bottom0;
}
#starRating .bottoms .garyBtn {
  margin-right57px!important;
}
#starRating .bottoms a {
  width130px;
  height35px;
  line-height35px;
  border-radius3px;
  display: inline-block;
  font-size16px;
  transition: all 0.2s linear;
  margin16px 0 22px;
  text-align: center;
  cursor: pointer;
}
.garyBtn {
  margin-right60px!important;
  background-color#e1e1e1;
  color#999999;
}
.blueBtn {
  background-color#1968b1;
  color#fff;
}
.blueBtn:hover {
  background#0e73d0;
}

关于使用jQuery怎么实现一个滑动星星评分效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×