Jquery - Hover() and mouseleave()

Asked

Viewed 268 times

2

I have a problem.

I have two div with a certain distance between them. I would like that when I place the mouse on the first div, the second div appears and I can pass the mouse from one to the other without the second div disappearing. Only disappear when the mouse is out of both.

I know I could put all this inside a larger div and schedule the event for that div. But in my application, the div that will appear in Hover() is in another part of the code and I need it to be so.

Follow the fiddle: https://jsfiddle.net/hbp7kfp2/3/

HTML

<div id="div1"></div>
<div id="div2"></div>

CSS

#div1 {
    width: 100px;
    height: 100px;
    background-color: red;
    margin: 10px;
    float:left;
}
#div2 {
    width: 100px;
    height: 100px;
    background-color: blue;
    margin: 10px;
    float: left;
    display: none;
}

JS

$('#div1, #div2').hover(function(){
    $('#div2').show();
});

$('#div1, #div2').mouseleave(function(){
    $('#div2').hide();
});

From now on, thank you.

  • so your code already does this, when you put the mouse between the two squares(Divs) it is in this condition(outside of both), you want the program to do ?

  • @Sneepsninja, that’s right. I think that would be the proper way. How do I do it?

2 answers

2


With delay resolves like this:

    var vermelho = 0;
var azul = 0;
$('#div2').hover(function(){
    $('#div2').show();   
    azul = 1;
});

$('#div1').hover(function(){
    $('#div2').show();
    vermelho =1;    
});

function fechar(){    
    if(vermelho==0 && azul==0){ $('#div2').hide(); }    
}

$('#div1').mouseleave(function(){    
    vermelho=0;
    setTimeout(function(){ fechar(); },1000);    
});

$('#div2').mouseleave(function(){    
    azul=0;
    setTimeout(function(){ fechar(); },1000);    
});
  • I forgot to comment in the code the value 1000 is equal to 1 second (1000 million second) if you need to adjust the delay

  • It worked perfectly. I need to do something here to increase your reputation?

  • 1

    https://jsfiddle.net/hbp7kfp2/5/

1

jQuery:

$('#div1').hover(function() {
  $('#div2').show();
}).mouseout(function() {
  $('#div2').show();
});

$('#all').mouseleave(function() {
  $('#div2').hide();
});

CSS:

#all {
  display: table;
}
#div1 {
  width: 100px;
  height: 100px;
  background-color: red;
  margin: 10px;
  float: left;
}
#div2 {
  width: 100px;
  height: 100px;
  background-color: blue;
  margin: 10px;
  float: left;
  display: none;
}

HTML:

<div id="all">
  <div id="div1"></div>
  <div id="div2"></div>
</div>
  • @re22, I’m sorry. But I don’t agree with you in editing my answer, since the author of the question already had the jQuery library in his code. If you are going to fix this every time you can open all topics and put. Unnecessary!

  • You can reverse any editing done in your answers. I include jQuery by you post the code as snippet, if to post something that does not work in the browser then use the normal code block.

  • Colleague, the Jquery added by @re22 to the example is to work, the Snippets are to play code, if you won’t run anything, then don’t use the snippet use the icon { }, found the edit favorable to the post. I removed the snippet that is not needed in the example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.