Reputation: 15
I'm trying to color between 1 and 5 elements on mouse hover, based on data-rating tag. I get the data correctly, but several things happen:
The HTML portion
<h2>
<i class="far fa-star" data-rating="1">1</i>
<i class="far fa-star" data-rating="2">2</i>
<i class="far fa-star" data-rating="3">3</i>
<i class="far fa-star" data-rating="4">4</i>
<i class="far fa-star" data-rating="5">5</i>
</h2>
The jQuery portion:
$('[class="far fa-star"]').mouseenter(function() {
var target = parseInt($(this).data('rating'));
for (i = 0; i < target; i++) {
$(this).parent().children(i).css('background-color', 'yellow');
}
});
$('[class="far fa-star"]').mouseleave(function() {
var target = parseInt($(this).data('rating'));
for (i = 4; i > target; i--) {
$(this).parent().children(i).css('background-color', 'transparent');
}
});
fiddle is here - fiddle
Upvotes: 0
Views: 480
Reputation: 1091
I think this is the effect you are trying to achieve:
$('[class="far fa-star"]').mouseenter(function () {
var target = parseInt($(this).data('rating'));
for (i = 0; i < target; i++) {
$(this).parent().children().eq(i).css('background-color', 'yellow');
}
});
$('[class="far fa-star"]').mouseleave(function () {
var target = parseInt($(this).data('rating'));
for (i = 0; i < target; i++) {
$(this).parent().children().eq(i).css('background-color', 'transparent');
}
});
Upvotes: 0
Reputation: 2829
I think the others didn't understand what you really want, I understood from your question that you want a highlighting feature for your rating system, here is an example
// using event delegation to get the current mouse hovered star
document.querySelector("h2").onmouseover = function(e) {
// only if the element is of type `<i>`
if(e.target.nodeName === "I") {
// get the rating
var rating = e.target.getAttribute("data-rating");
// looping over the `<i>` elements and color them the correct way
// so only from 0 to the current rating are yellow and the rest
// are black and that makes sure the highlighting is updated even
// if the user keeps moving over the stars
Array.prototype.forEach.call(this.children, (c, i) => c.style.color = i < rating ? "yellow" : "black");
}
}
// reset the color of all the stars
document.querySelector("h2").onmouseleave = function() {
Array.prototype.forEach.call(this.children, c => c.style.color = "black");
}
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<h2>
<i class="fas fa-star" data-rating="1"></i>
<i class="fas fa-star" data-rating="2"></i>
<i class="fas fa-star" data-rating="3"></i>
<i class="fas fa-star" data-rating="4"></i>
<i class="fas fa-star" data-rating="5"></i>
</h2>
Upvotes: 1
Reputation: 2712
Looking at your code, it can be achieved by CSS. Please check this question s approved.
Upvotes: 0
Reputation: 47
You Can Use Background Color in css with :hover psudo class
.yourclass:hover{
background-color: yellow;
}
Or Use add event listener
Upvotes: 0
Reputation: 47
You Can Use Background Color in css with :hover psudo class
.yourclass:hover{
background-color: yellow;
}
Upvotes: 1