我有一個由jquery懸停函數執行的重複CSS動畫的問題。您可以在此DEMO中看到問題的示例。用jquery懸停重複的CSS動畫
當您打開演示程序時,請將鼠標懸停在第一顆星上,並從左向右平移鼠標。正如你所看到的動畫重複造成口吃。我該如何解決這個問題,以便動畫只對每顆恆星發射一次,保持以前的恆星高亮顯示。
HTML
<div class="GvStarContainer">
<!--Style 1 STARTED-->
<div class="GvStarTmp">
<div class="margi-star">
<div class="rate-ex1-cnt">
<div id="1" class="star star-one-1 rate-btn star-one"></div>
<div id="2" class="star star-one-2 rate-btn star-one"></div>
<div id="3" class="star star-one-3 rate-btn star-one"></div>
<div id="4" class="star star-one-4 rate-btn star-one"></div>
<div id="5" class="star star-one-5 rate-btn star-one"></div>
</div>
</div>
</div>
<!--Style 1 FINISHED-->
<!--Style 2 STARTED-->
<div class="GvStarTmp">
<div class="margi-star">
<div class="rate-ex2-cnt">
<div id="1" class="star star-two-1 rate-btn star-two"></div>
<div id="2" class="star star-two-2 rate-btn star-two"></div>
<div id="3" class="star star-two-3 rate-btn star-two"></div>
<div id="4" class="star star-two-4 rate-btn star-two"></div>
<div id="5" class="star star-two-5 rate-btn star-two"></div>
</div>
</div>
</div>
<!--Style 2 FINISHED-->
<!--Style 3 STARTED-->
<div class="GvStarTmp">
<div class="margi-star">
<div class="rate-ex3-cnt">
<div id="1" class="star star-tree-1 rate-btn star-tree"></div>
<div id="2" class="star star-tree-2 rate-btn star-tree"></div>
<div id="3" class="star star-tree-3 rate-btn star-tree"></div>
<div id="4" class="star star-tree-4 rate-btn star-tree"></div>
<div id="5" class="star star-tree-5 rate-btn star-tree"></div>
</div>
</div>
</div>
<!--Style 3 FINISHED-->
</div>
JS
$(document).ready(function() {
var prevStars = $(this).prevAll();
var nextStars = $(this).nextAll();
$(".star").hover(
function() {
var prevStars = $(this).prevAll();
prevStars.addClass('rate-btn-hover');
},
function() {
var prevStars = $(this).prevAll();
prevStars.removeClass('rate-btn-hover');
}
);
$("body").on("click", ".star", function() {
var prevStars = $(this).prevAll().addBack();
prevStars.addClass('rate-btn-active');
});
});
你在添加和刪除類之前可能會添加一些超時。 –