2017-08-07 76 views
0

我想在mouseover上交换多个图像,并且正在寻找一种方法来执行此操作。我期待着'点亮'多颗星星。下面的代码可以正常工作,但是当光标悬停在第三颗星上时,例如3颗星点亮的最佳方式是什么?在鼠标悬停时交换多个图像

<div class="rating" data-value="4"> 
<img src="star.png"/> 
<img src="star.png"/> 
<img src="star.png"/> 
<img src="star.png"/> 
</div> 

和JS:

$(".rating > img") .mouseover(function() { 
      this.src= "star-on.png"; 
     }).mouseout(function() { 
      this.src= "star.png"; 

}); 
+0

在我头顶上,你可以给每个班级设置一个类似“星级-1”......“星级-4”和循环的班级通过你想要突出的所有星星。 – Zachooz

回答

2

您可以通过获取这是将鼠标停留在集内恒星的索引,然后基于该指数更换的星星正确的量做到这一点:

$(".rating > img").mouseover(function() { 
 
    // Get the index of the star that is being hovered over 
 
    var idx = $("img").index(this); 
 
    
 
    // Loop only over the stars that should be highlighted and highlight them. 
 
    // Use the index to know how many to loop over 
 
    for(var i = 0; i < idx + 1; i++){ 
 
     // "Light up" the star: 
 
     $(".rating > img")[i].src= "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d8/Full_Star_Blue.svg/2000px-Full_Star_Blue.svg.png"; 
 
    } 
 
    }).mouseout(function() { 
 
     
 
    // Turn off all stars 
 
    $(".rating > img").each(function(){ 
 
    this.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png" 
 
    }); 
 

 
});
img { 
 
width:30px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="rating" data-value="4"> 
 
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png"> 
 
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png"> 
 
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png"> 
 
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Empty_Star.svg/1024px-Empty_Star.svg.png"> 
 
</div>

0

我禾ULD只是想提一提,这是很容易achieveable纯CSS和Flexbox的特点:

* {box-sizing:border-box;} 
 

 
body {padding:30px;} 
 

 
.stars { 
 
    padding:10px; 
 
    background:#fff; 
 
    display:flex; 
 
    flex-direction:row-reverse; 
 
} 
 

 
.stars .star { 
 
    flex: 0 1 50px; 
 
    background:red; 
 
    padding:20px; 
 
} 
 

 
.stars .star:hover, .stars .star:hover ~ .star { 
 
    background:salmon; 
 
}
<div class="stars"> 
 
    <div class="star">4</div> 
 
    <div class="star">3</div> 
 
    <div class="star">2</div> 
 
    <div class="star">1</div> 
 
</div>

+0

请不要在第三方链接发布代码,因为这些链接可能会随着时间的推移而中断。只需在这里创建一个代码片段即可。 –

+0

此解决方案不需要Flexbox。 –

+0

当然不是必需的,但在这种特殊情况下是一种很好的做法。文体方面应该在样式表中定义,所以我想要展示更好更简单的解决方案,而不是仅仅严格要求OP标签,因为它更具有教育意义。 –

0

下面是普通的香草JavaScript的一个例子:

var stars = document.querySelector('.stars') 
 

 
stars.addEventListener('mouseover', function(event) { 
 
    var target = event.target 
 
    var index = target.dataset.index 
 

 
    for (var i = 0; i < 5; i++) { 
 
    var star = stars.querySelector('[data-index="' + i + '"]') 
 
    star.classList.toggle('active', i <= index) 
 
    } 
 
}) 
 

 
stars.addEventListener('mouseleave', function(event) { 
 
    for (var i = 0; i < 5; i++) { 
 
    var star = stars.querySelector('[data-index="' + i + '"]') 
 
    star.classList.toggle('active', false) 
 
    } 
 
})
.star { 
 
    display: inline-block; 
 
    width: 20px; 
 
    height: 20px; 
 
    border: 1px solid black; 
 
    background: white; 
 
} 
 

 
.star.active { 
 
    background: yellow; 
 
}
<div class="stars"> 
 
    <div class="star" data-index="0"></div> 
 
    <div class="star" data-index="1"></div> 
 
    <div class="star" data-index="2"></div> 
 
    <div class="star" data-index="3"></div> 
 
    <div class="star" data-index="4"></div> 
 
</div>

+0

请勿将代码发布到第三方网站,因为这些链接可能会随着时间的推移而中断。只需在这里创建一个“代码片段”,任何人都需要了解您的答案就在这里,在一个地方。 –

+0

@ScottMarcus gotcha –

+0

您可能还需要考虑在语句的末尾添加分号,而不是依赖JS运行库来执行它(它具有众所周知的边界情况,它会导致错误并导致错误)。 –