Reputation: 365
I'm trying to do a very simple system
My intention is to finish this system, my knowledge is very limited in javascript.
mouseover: function(){
// remove hover
t.rate.eventDrain();
// fill hover
t.rate.eventFill($(this));
},
mouseout: function(){
t.rate.eventDrain();
},
My Question: How to make so that you can edit the score whenever you want, but if there was no change in the mouseover leave the previous score.
Upvotes: 0
Views: 1233
Reputation: 2702
You need to save selection state with data-selected
attribute used on clicked star. This will allow you to return to pre-mouseover selection state on mouseout
event
So, here is the code:
<div id="rating">
<a class="star"></a>
<a class="star"></a>
<a class="star"></a>
<a class="star"></a>
<a class="star"></a>
<!-- as many as you need -->
<input id="getRating" type="button" value="Rate!"></input>
</div>
a.star {
display:block;
height: 20px;
width: 20px;
background: url(http://sc.cuevana.tv/new/img/rate_list.png);
float: left;
cursor: pointer;
}
a.star.hover {
background-position: 0 -20px;
}
a.star.rated {
background-position: 0 -40px;
}
$(document).on("click", ".star", function (e) {
//clearing currrently "rated" star
$(".star").removeAttr("data-selected");
var $this = $(this);
//un-"rating" all the following stars
$this.nextAll(".star").removeClass("rated");
//mark clicked star with data-selected attribute
$this.addClass("rated").attr("data-selected", "true");
//mark previous stars
$this.prevAll(".star").addClass("rated");
});
$(document).on("mouseover", ".star", function (e) {
//unmark rated stars
$(".star").removeClass("rated");
var $this = $(this);
//mark currently hovered star as "hover"
$(this).addClass("hover");
//mark preceding stars as "hover"
$this.prevAll(".star").addClass("hover");
});
$(document).on("mouseout", ".star", function (e) {
//un-"hover" all the stars
$(".star").removeClass("hover");
//mark star with data-selected="true" and preceding stars as "rated"
$("[data-selected='true']").addClass("rated").prevAll(".star").addClass("rated");
});
$(document).on("click", "#getRating", function (e) {
//wise comment here
var rating = $(".star.rated").length;
alert(rating);
});
Upvotes: 1
Reputation: 519
If I understand correctly you want to be able to "re-vote" if needed.
You are going to need to add a few if statements, but this should do the trick. It's messy, but I was in a hurry.
mouseover: function(){
if(!t.rate){
t.rate.eventDrain();
}else{
t.rate.eventFill($(this));
}`
Please note that the below is the incomplete function - just provided for example:
click: function(){
if($(this).prevAll().attr("class") == 'rated hover')
{
$("div.puntuar a").removeClass('rated');
$(this).add($(this).prevAll()).addClass('rated');
}else{
$(this).add($(this).prevAll()).addClass('rated');
}
Upvotes: 0