开发者

jQuery Animate to Most Recent Hover Only

开发者 https://www.devze.com 2023-03-21 22:51 出处:网络
I have a carousel that is linked to a list of items, and when you hover over each of those items, it animates a carousel to a distinct position.

I have a carousel that is linked to a list of items, and when you hover over each of those items, it animates a carousel to a distinct position.

The problem is if I rapidly hover over a bunch of those list items, and it will trigger the animate to each of those in the order in which they were hovered. What I want is for it to automatically adjust to whichever is开发者_开发百科 the most recent hover, so that there is not a bunch of unnecessary movement going on.

Here is the jQuery for the hover:

$('#item1 a').hover(function() {
    $('#hero-slider ul').animate({
        right: '0'
    }, 500);
});
$('#item2 a').hover(function() {
    $('#hero-slider ul').animate({
        right: '980'
    }, 500);
});
$('#item3 a').hover(function() {
    $('#hero-slider ul').animate({
        right: '1960'
    }, 500);
});
$('#item4 a').hover(function() {
    $('#hero-slider ul').animate({
        right: '2940'
    }, 500);
});

If I hover over all of those items in rapid succession, it will move to all their positions before stopping.


try adding .stop() to the function.

 $('#carProd4 a').hover(function() {
     $('#hero-slider ul').stop().animate({
         right: '2940'
     }, 500);
0

精彩评论

暂无评论...
验证码 换一张
取 消