简体   繁体   English

jQuery在悬停错误上上下滑动

[英]jQuery slide up/down on hover bug

I have one large div with one smaller div inside it. 我有一个大div,里面有一个小div。 The smaller div is at first displayed as hidden. 较小的div首先显示为隐藏。 When the user hovers the mouse over the large container-div the smaller div is animated in with the show/hide-functions. 当用户将鼠标悬停在较大的容器div上时,较小的div将使用显示/隐藏功能进行动画处理。 So far everything works fine. 到目前为止,一切正常。

However. 然而。 The smaller div is animated in from the bottom - so if I let the cursor hover over the container at the very bottom, it's hovering over the animation of the smaller div while it's sliding in. So now the cursor is hovering over the smaller div instead of the larger div, and thus triggering the hide-function on the smaller div. 较小的div从底部开始进行动画处理-因此,如果我将光标悬停在最底部的容器上,则它会在滑入时在较小的div动画上悬停。因此,现在光标正在悬停在较小的div上较大div的,从而触发较小div的隐藏功能。 This creates an infinite loop of show/hide calls as the smaller div slides in and out of where the cursor is pointing. 当较小的div滑入和滑出光标指向的位置时,这将导致显示/隐藏调用无限循环。

Any ideas how to avoid this and not break the hovering on the container as the smaller div enters? 有什么想法如何避免这种情况并且在较小的div进入时不破坏容器的悬停?

This is what I mean: 这就是我的意思:

alt text http://archivedworks.com/fileserver/jQuery_hover_showhide.jpg 替代文字http://archivedworks.com/fileserver/jQuery_hover_showhide.jpg

You can do it like this: 您可以这样做:

$("#outerDiv").hover(function() {
  $("#innerDiv:hidden").slideDown(); //your show code
}, function() {
  $("#innerDiv:visible").slideUp(); //your show code
});

This only queues a hide animation if it's visible ( :visible ), and only queues a show animation if it's not ( :hidden ), preventing the queue/loop behavior you have now. 如果隐藏动画是可见的( :visible ),则仅将其排队;如果:hidden动画( :hidden )不:visible ,则仅将其进行排队,以防止出现队列/循环行为。

$('#idOfLargeDiv').hover(
    function() {
        $('#idOfSmallDiv').slideDown();
    }, 
    function() {
        $('#idOfSmallDiv').slideUp();
    }
);

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

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