2013-02-18 221 views
0

我正在寫卡列表。
卡列表中的li元素既有mouseenter也有mouseleave事件。
重疊jquery鼠標懸停事件

mouseenterCard: function(index) { 
    var nOnRight = index+2; 
    var n = index+1; 

    if (n!=1) { 
     $('#cards-list li:nth-child('+n.toString()+')').animate({'margin-left': '30px'}, 
                   "fast", 
                   function() { 

                   }); 
    } 
    $('#cards-list li:nth-child('+nOnRight.toString()+')').animate({'margin-left': '30px'}, 
                    "fast"); 
}, 
mouseleaveCard: function(index) { 
    var nOnRight = index+2; 
    var n = index+1; 
    if (n!=1) { 
     $('#cards-list li:nth-child('+n.toString()+')').animate({'margin-left': marginLeft.toString()+'px'}, 
                   "fast", 
                   function() { 

                   }); 
    } 
    $('#cards-list li:nth-child('+nOnRight.toString()+')').animate({'margin-left': marginLeft.toString()+'px'}, 
                    "fast"); 
} 

$('#cards-list').on('mouseenter', 'li' ,function(e){ 
    CardList.getInstance().mouseenterCard($(this).index()); 
}); 

$('#cards-list').on('mouseleave', 'li' ,function(e){ 
    CardList.getInstance().mouseleaveCard($(this).index()); 
}); 

這裏是DEMO
正如你兩個元素之間交換快,li元素的行爲怪異。
問題是什麼?

回答

0

多次移動相同元素時,動畫會排隊。您有一定.stop.finish停止動畫(後者結束動畫即時):

$('#cards-list li:nth-child('+nOnRight.toString()+')').stop().animate(...