简体   繁体   English

jQuery mouseenter和mouseleave问题,保持切换div显示

[英]jQuery mouseenter and mouseleave problems, keep toggled div shown

I have got 1 area, and on mouseenter I am fading in the following div: 我有一个区域,在mouseenter上我在以下div中消失:

$( "area.hasmore" ).mouseenter(function() {
  $(this).next(".popup").stop().fadeIn();
});

Let's say the popup appears on the right hand side. 假设弹出窗口出现在右侧。 What if the user leaves the area on the left hand side? 如果用户离开左侧区域怎么办? Let's fade that popup out: 让我们淡出那个弹出窗口:

$( "area.hasmore, .popup" ).mouseleave(function() {
  $(".popup").fadeOut(); 
});

Here comes my problem: users should be able to move the cursor into the fresh opened popup to the right and maybe even click a link in there. 这就是我的问题:用户应该能够将光标移动到右边的新打开的弹出窗口,甚至可以单击其中的链接。 My problem is that it fades out due to the mouseleave event on the area. 我的问题是它由于该区域上的mouseleave事件而淡出。 One problem might be the fact that the popup is no child. 一个问题可能是弹出窗口不是孩子。 As a child of the area hovering the popup would still count as being 'inside' the area I guess. 作为该区域的孩子,在弹出窗口中我仍然可以算作“内部”区域。 So I am kind of trying to find out how to keep the popup visible when mouseentering it and mouseleaving the area. 所以我试图找出如何在鼠标输入和鼠标移动区域时保持弹出窗口可见。 Here's the code: 这是代码:

<area class="hasmore" />
<div class="popup">...

Sry if I missed a question where this exact problem is being discussed. 如果我错过了一个问题,正在讨论这个问题。

jsfiddle here: fiddle jsfiddle在这里: 小提琴

You could manage what is visible in the hover instead of mouseenter and mouseleave : 您可以管理hover可见的内容而不是mouseentermouseleave

Something like this: 像这样的东西:

$('div').hover(function () {
    console.log(this.className);
    if (this.className === 'hasmore') {
        $(this).next(".popup").stop().fadeIn();
    } else if (this.className !== 'hasmore' && this.className !== 'popup') {
        $(".popup").fadeOut();
    }
});

Here is a fiddle demonstrating 这是一个小提琴演示

html HTML

<div class="hasmore">hover me</div>
<div class="popup">Popup 1</div>

<div class="evenmore">stuff</div>
<div class="popup">2nd popup. don't mind me</div>

Javascript 使用Javascript

 $( ".container" ).mouseenter(function() {
     $(".popup").stop().fadeIn();
 });

$( "div.container" ).mouseleave(function() {
    $(".popup").fadeOut();
});

CSS (include this) CSS(包括这个)

.container {
    display: block;
    line-height: 1em;
    margin: 0;
    padding: 0;
}

The trick is to create a div (.container) with display: block and enclosure .hasmore and .popup inside it! 诀窍是创建一个div(.container),其中包含display:block和enclosure .hasmore和.popup!

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

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