I am trying to make scores with stars and that when passing over each one, a css class is applied. The fact is that it applies to all the stars at once and my intention is that it only applies when you pass the mouse over from beginning to end and that they remain fixed as the score goes up and only while you have the mouse on top.
Here is what I have done.
var estrella = document.getElementsByClassName("star");
function inclinar() {
for(var i = 0; i < estrella.length; i++) {
estrella[i].className += " inclinada";
}
}
function desinclinar() {
for(var i = 0; i < estrella.length; i++) {
estrella[i].classList.remove("inclinada");
}
}
for(var i = 0; i < estrella.length; i++) {
estrella[i].addEventListener('mouseenter', inclinar);
estrella[i].addEventListener('mouseleave', desinclinar);
}
.stars ul {
list-style-type:none;
padding:0;
}
.stars ul > li.star {
display:inline-block;
font-size:2.5em;
color:#ccc;
}
.stars ul > li.star.inclinada {
color:#FFCC31;
transform: rotate(20deg);
}
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet"/>
<div class="stars text-center">
<ul id="stars">
<li class="star" data-value="1">
<i class="fas fa-star"></i>
</li>
<li class="star" data-value="2">
<i class="fas fa-star"></i>
</li>
<li class="star" data-value="3">
<i class="fas fa-star"></i>
</li>
<li class="star" data-value="4">
<i class="fas fa-star"></i>
</li>
<li class="star" data-value="5">
<i class="fas fa-star"></i>
</li>
</ul>
</div>
Thank you!