简体   繁体   English

重叠的jquery mouseover事件

[英]Overlapping jquery mouseover event

I am writing a card list. 我正在写一张卡片清单。
The li elements inside the card list has both mouseenter and mouseleave event. 卡列表中的li元素同时具有mouseenter事件和mouseleave事件。

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());
});

Here is the DEMO 这是演示
As you swap between two elements quickly, the li elements behave weird. 当您在两个元素之间快速交换时,li元素的行为很奇怪。
What is the problem? 问题是什么?

The animation gets queued as you move over the same element multiple times. 多次在同一元素上移动时,动画会排队。 You have to stop the animation with either .stop or .finish (latter ends the animation instantly): 您必须使用.stop.finish停止动画(后立即结束动画):

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

You can try debouncing/throttling the events. 您可以尝试对事件进行反跳/限制。 http://benalman.com/projects/jquery-throttle-debounce-plugin/ http://benalman.com/projects/jquery-throttle-debounce-plugin/

If You have running animations, use .stop() on the mouseleave event to break the animation 如果您正在运行动画,请在mouseleave事件上使用.stop()来中断动画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM