views:

1329

answers:

3

Hi Guys I have this little Jquery script: link text

$(document).ready(function() 
{
      $('#image p').hide();

      $('img').hover(function()
      {
            $('#image p').show(200); 
      }, function()
      {
            $('#image p').hide(200); 
      });
});

I works fine, but I want to be able to hover over the Text located in the Image, every time I try, it just keeps "bouncing"

Any help is much appreciated, thanks, Keith

A: 

Thanks for the reply,

that does what I already have. What I want is when I hover over the paragraph, that it stays there, and not "bounce" about

cheers, Keith

Keith Donegan
+10  A: 

Good question.

The problem seems to be that when the mouse is over the paragraph, the mouse is no more over the image. So the paragraph is hidden. When the paragraph is hidden, the mouse is again over the image, and so the paragraph is shown again. And so on...

A good solution is to use mouseenter and mouseleave events instead of mouseover and mouseout:

$(document).ready(function(){
    $('#image p').hide();

    $('#image').bind("mouseenter", (function(){
        $('#image p').show(200)
     }));

    $('#image').bind("mouseleave", (function(){
        $('#image p').hide(200)
     }));

});

The major difference between mouseenter/mouseleave events and mouseover/mouseout events is that the former don't bubble.

In this example, the child paragraph of div#image still receive the mouseenter/mouseleave events (even if you aren't listening for them) but the events won't bubble up to their parent element. See this page fore extended discussion on it.

You also have to assign the event no more to the img tag, but to the containing div. It should not be a problem.

alexmeia
Why -1? Please explain.
alexmeia
The idea is fine, but provided code does not work.The last event handler must be specified like this:$('#image p').mouseover(function () { $(this).show()}
Marko Dumic
You are right. It was a cut and paste error. The last event was not necessary, anyway.
alexmeia
+1 nice explication
Daok
good! removed -1 and voted it up. regards!
Marko Dumic
@Marko Dumic - that makes sense, if there were 2 paragraphs, they would both show up. The $(this).show() is better alright, thanks
Keith Donegan
A: 

That is absolutely perfect Alexmeia, exactly what I need. Can I ask you what's the difference between using bind and hover? thanks again, Keith.

Also, I haven't got a clue why you have a -1, I can't give rep yet, somebody else must have done that. It doesn't make sense as the solution is correct

Keith Donegan
I copied and paste from a similar solution I had, and I put in the code a last function that is not necessary. I updated my answer.
alexmeia