简体   繁体   English

jQuery中的星级

[英]Star Rating in Jquery

I want a star rating system please check the JSFiddle .I want to stick the rating on mouse click. 我想要一个星级评分系统,请检查JSFiddle。我想在鼠标单击时保留评分。

HTML: HTML:

<div class="rating-star rating-star-off" data-value="0"></div>
<div class="rating-star rating-star-off" data-value="1"></div>
<div class="rating-star rating-star-off" data-value="2"></div>
<div class="rating-star rating-star-off" data-value="3"></div>
<div class="rating-star rating-star-off" data-value="4"></div>
</div>

CSS: CSS:

.rating-star{width:25px;height:25px;float:left;margin-right:5px;}
.rating-star-off{background:#eee url(star-off.svg) no-repeat;}
.rating-star-on{background:#000 url(star-on.svg) no-repeat;}

jQuery: jQuery的:

$(function(){

  $(".rating-star").click(function(e){
    $(this).prevAll().andSelf().addClass('rating-star-on');
    $(this).nextAll().removeClass('rating-star-on');
  });
  $(".rating-star").hover(function(){
    $(this).prevAll().andSelf().addClass('rating-star-on');
    $(this).nextAll().removeClass('rating-star-on');
  });
  $(".rating-star").mouseout(function(){
    $(".rating-star").removeClass('rating-star-on'); 
  });
});

http://jsfiddle.net/piyush_dezi/a67gv2o7/ http://jsfiddle.net/piyush_dezi/a67gv2o7/

Thanks in advance for your help. 在此先感谢您的帮助。

One way is to add another class for selected to your css rules 一种方法是将另一个selected类添加到CSS规则

.rating-star-on, 
.rating-star-selected
    {background:#000 url(star-on.svg) no-repeat;}

And change the click handler to add this selected class. 并更改单击处理程序以添加此选定的类。

$(".rating-star").click(function(e){
    $(this).prevAll().andSelf().addClass('rating-star-selected');
    $(this).nextAll().removeClass('rating-star-on');
});

You will likely need to make some adjustments if you want user to be able to change their rating also 如果您希望用户也能够更改其评分,则可能需要进行一些调整。

DEMO DEMO

First your click event add classes to highlight the stars after it mouse out event remove these classes. 首先,单击事件添加类,以在鼠标移出事件删除这些类后突出显示星星。 In mouse click event unbind mouse out event so it will not remove classes from your stars. 在鼠标单击事件中,取消绑定鼠标移出事件,这样就不会从星星中删除类。 Also instead of using anonymous mouse out function use name function so your can bind it again for other starts. 另外,不要使用匿名鼠标移出功能,而是使用名称功能,这样您就可以再次将其绑定以用于其他启动。

   $(function(){
$(".rating-star").mouseout(mouseOutFunction);

function mouseOutFunction(){
$(".rating-star").removeClass('rating-star-on'); 
}


$(".rating-star").click(function(e){
$(this).prevAll().andSelf().addClass('rating-star-on');
$(this).nextAll().removeClass('rating-star-on');
$(".rating-star").bind( "mouseout", mouseOutFunction);
$(this).unbind( "mouseout" );
});

$(".rating-star").mouseenter(function(){

  $(this).prevAll().andSelf().addClass('rating-star-on');
  $(this).nextAll().removeClass('rating-star-on');
});

});

Add this line to CSS: 将此行添加到CSS:

.rating-star-hover {background:#000 url(star-on.svg) no-repeat;}

Update JS: 更新JS:

$(this).prevAll().andSelf().removeClass('rating-star-off').addClass('rating-star-on');
$(this).nextAll().removeClass('rating-star-on');
});

$(".rating-star").hover(function(){
  $(this).prevAll().andSelf().addClass('rating-star-hover');
  $(this).nextAll().removeClass('rating-star-on');
});

$(".rating-star").mouseout(function(){
    $(".rating-star").removeClass('rating-star-hover'); 
});

The reason why it's not getting executed is that when we click on the div, the rating-on class is added but as we mouseout the class is been removed. 它没有得到执行的原因是,当我们单击div时,添加了rating-on类,但是当我们将其移出鼠标时,该类被删除了。 So the rating class is not changed. 因此评级等级不会改变。 You can also do this using an boolean variable. 您也可以使用布尔变量来执行此操作。 Check this : 检查一下:

    `http://jsfiddle.net/bandhavya/we3mx6vt/`

Why you dont use jquery rating plugin ? 为什么不使用jQuery Rating插件? Have many options in the web. 网络上有很多选择。 Like raty.check this url: Rating Plugins Raty is one of the best that i used. 像raty.check这样的URL: Rating Plugins Raty是我使用过的最好的插件之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM