简体   繁体   English

仅当未触发另一个mouseover事件时,如何才能为jquery mouseout事件创建异常?

[英]How can I create an exception for a jquery mouseout event only if another mouseover event hasn't been triggered?

Basically my client wants hidden navigation to appear when mouseover an image. 基本上,我的客户希望将鼠标悬停在图像上时显示隐藏的导航。 I've solved the problem of the navigation not hiding when you mouseover the navigation and then hiding when you leave the navigation. 我已经解决了以下问题:当您将鼠标悬停在导航上时导航不会隐藏,而在离开导航时会隐藏。 There are two problems I'm running into and I've tried a variety of different combinations that I thought would work, but of course didn't. 我遇到了两个问题,我尝试了各种我认为可行的不同组合,但是当然没有。 The two problems are: 两个问题是:

  1. When you mouseout the image without mouseover the navigation then the navigation needs to hide, as of right now it stays open until you either mouseover the image again or mouseleave the navigation. 当您将鼠标悬停在图像上而没有将鼠标悬停在导航上时,则导航需要隐藏,从现在起它一直保持打开状态,直到您再次将鼠标悬停在图像上或将鼠标移开导航。

  2. Second problem is when you mouseleave the navigation directly to mouseover the image it loops the function and hides the nav then opens the nav again, I've tried changing slideToggle to show, but that causes a whole bunch of other issues. 第二个问题是,当您直接将导航鼠标悬停在鼠标悬停在图像上时,它会循环播放功能并隐藏导航,然后再次打开导航,我尝试更改slideToggle以显示,但是这会导致很多其他问题。

Right now the code is behaving as close to how I want it and could be considered acceptable, but I'd love to know how to solve the problems above. 现在,代码的行为与我想要的代码非常接近,可以被认为是可以接受的,但是我很想知道如何解决上述问题。 I thought about using the hoverIntent plugin to sense the mouse movements and only trigger the functions once the mouse has slowed, but couldn't get it working properly. 我考虑过使用hoverIntent插件来感测鼠标的移动,并且仅在鼠标放慢速度后才触发功能,但无法使其正常工作。 Clearly, I am a novice when it comes to javascript and jquery so please forgive me, but I'd really appreciate any help. 显然,关于javascript和jquery,我是一个新手,因此请原谅我,但我非常感谢您的帮助。

Here is my code 这是我的代码

$(document).ready(function(){

$(".nav-body").hide();

$(".nav-head").mouseover(function(){
$(this).next(".nav-body").slideToggle(600);
$(".nav-body").mouseleave(function(){
$(this).hide(700);
});
});

});

Here is my html: 这是我的html:

<p class="nav-head"><img src="/images/face-btn.jpg" /></p> 
<div class="nav-body"> 
<ul><?php wp_list_pages('title_li=&child_of=12&depth=1'); ?></ul> 
</div>

Markup change 标记变更

<div class="nav-container">
    <p class="nav-head"></p>
    <div class="nav-body"></div>
</div>

Javascript 使用Javascript

var eventHandler;

eventHandler = function(){$(".nav-head").one("mouseover",function(){
   $(this).next(".nav-body").slideToggle(600);
    $(".nav-container").one("mouseleave", function(){
        $(this).find(".nav-body").hide(700, eventHandler);
    });
});};

eventHandler();

The first change is from mouseleave to mouseout. 第一个变化是从mouseleave到mouseout。 Inside the navigation, there are likely to be descendent elements that cover the actual nav-body. 在导航内部,可能有后代元素覆盖实际的导航实体。 With mouse leave, the handler only triggers when the mouse leaves the bound element. 使用鼠标离开时,处理程序仅在鼠标离开绑定元素时触发。 If it goes over descend it elements, it is considered leaving. 如果它下降超过它的元素,则视为离开。 Mouseout only triggers if it is outside the bounds of the bound object. 仅当鼠标移出绑定对象的边界时,它才会触发。

The second thing I did was assign a delegate to the handler binding operation so that I could use it as a callback function for hide(). 我做的第二件事是为处理程序绑定操作分配了一个委托,以便可以将其用作hide()的回调函数。 This way, the event handler won't be restored to the nav-head until the hide is completely done. 这样,在隐藏完成之前,事件处理程序不会恢复到导航头。

The last was to assign the mouseout handler to the containing div. 最后是将mouseout处理程序分配给包含的div。 This way, the so long as it leaves the nav-head (or the nav-body) since its contained, the body will hide. 这样一来,只要它离开导航头(或导航体),因为它被包含,它就会隐藏。

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

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