Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery on hover and out

I am trying to hide a div whenever I will hover over it and show another one in the same place.. And when I take the mouse out of that.. the previous div will be shown and this div will be hidden...

$(document).ready(function(){



      $('#hover_tutor').hover(
         function () {
           $('#hover_tutor').hide();
           $('#hover_tutor_hidden').show();
         }, 
         function () {
           $('#hover_tutor').show();
         $('#hover_tutor_hidden').hide();
         }
     );

   });



  <div id="hover_tutor">Blah</div>
  <div id="hover_tutor_hidden" style="display:none;">Bleh</div>

But on hovering the hover_tutor... something is happening.. It's jumping up and down.. I don't know what's wrong...

like image 339
marukobotto Avatar asked Jan 27 '15 05:01

marukobotto


People also ask

What is the jQuery equivalent of Onmouseover?

jQuery mouseover() Method The mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.

What is the difference between Mouseout and Mouseleave?

This means that mouseleave is fired when the pointer has exited the element and all of its descendants, whereas mouseout is fired when the pointer leaves the element or leaves one of the element's descendants (even if the pointer is still within the element).

What is Mouseout in jQuery?

jQuery mouseout() MethodThe mouseout event occurs when the mouse pointer leaves the selected element. The mouseout() method triggers the mouseout event, or attaches a function to run when a mouseout event occurs.

What is jQuery hover?

jQuery hover() Method The hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.


3 Answers

You need to use .mouseenter event for #hover_tutor div and .mouseleave for #hover_tutor_hidden div:

$('#hover_tutor').mouseenter(function () {
       $(this).hide();
       $('#hover_tutor_hidden').show();
     });

 $('#hover_tutor_hidden').mouseleave(function () {
       $('#hover_tutor').show();
     $(this).hide();
     }
 ).mouseleave();//trigger mouseleave to hide second div in beginning 

Working Demo

like image 167
Milind Anantwar Avatar answered Oct 16 '22 09:10

Milind Anantwar


You can also use toggle method instent of hide/show on hover event

<div id="hover_tutor" style="display: block;">Blah</div>
<div id="hover_tutor_hidden" style="display: none;">Bleh</div>

  $('#hover_tutor').hover(
     function () {
      $('#hover_tutor').toggle();
       $('#hover_tutor_hidden').toggle();
     });

Working demo http://jsfiddle.net/ivyansh9897/8jgjvkqk/

like image 25
Shailendr singh Avatar answered Oct 16 '22 10:10

Shailendr singh


If you do have the flexibility to modify your html little bit using class attribute there's a better way. Use .toggle() to alter the state of your element on both mouseover and mouseleave.

HTML :

<div id="hover_tutor" class="hello">Blah</div>
<div id="hover_tutor_hidden" class="hello" style="display:none;">Bleh</div>

jQuery :

$(".hello").on("mouseover mouseleave", function(){
    $("#hover_tutor").toggle();
    $("#hover_tutor_hidden").toggle();
});

jsFiddle

like image 33
Md Ashaduzzaman Avatar answered Oct 16 '22 10:10

Md Ashaduzzaman