views:

292

answers:

2

Hello

I have following problem. I'm working on simple jquery tooltip and now I have to deal with some strange behavior of jQuery. Everytime when I mouseover the element, events for mouse over and mouse out are triggered both - so the tooltip disappears (but if I keep hand over, it does a lot of blinks in one sec). There's my code.

  var hint = $('<div id="hint-wrap"><div id="hintbox-top"><div id="hintbox-bottom"><div id="hintbox-topleft"><div id="hintbox-bottomleft"><div id="hint-innerwrap"><div id="hint"></div></div></div></div></div></div></div>'); // Funny I know :-D

  hint.hide();
  $('body').append( hint ); // I append it

  $('.hashint').hover(function(e){   

// Set position to cursor's
hint.css( 'left', e.pageX );
hint.css( 'top', e.pageY - 60 );

// animated append
hint.css( 'opacity', 0.2 );
hint.show();
hint.animate( { 'opacity' : 1 }, 100 );

// set text
$( "#hint" , hint).html( $( '#' + $(this).attr( 'id' ) + '-hint' ).html() ); 

}, function(){ // there is the problem hint.hide(); });

And the HTML:

<div id="usernamelabel-hint" class="hinttext">Lorem Ipsum is simply dummy text of the printing and type.Lorem. <a href="#">Sign up!</a></div> <!-- This is hint text (and code) -->
<p><label><span class="hashint" id="usernamelabel">Username</span><span class="asterisk">*</span></label> <!-- ... stuff --> </p>

Please, does anybody know why the mouse out event is still triggering and hiding my box?

Thanks a lot, Ondrej

+1  A: 

First, why don't you just put the HTML for the hint in the HTML document?
And in the CSS you just set display:none ?
Then when showing the hint, you can just use hint.show or hint.fadeIn.

I would just use $.mouseenter and $.mouseleave.

Example:

$.('#usernamelabel-hint').mouseenter(function() { 
  // show or fade inn hint text
});

$.('#usernamelabel-hint').mouseleave(function() {
  // Hide or fade out hinttext
});
Steven
Ok, I put the hint box into the document.I'm not using fadeIn cause I want to start on opacity 0.2 :o) That doesn't matter anyway.And the mouseenter and mouseleave events makes me the same problmes as with hover. (And in my exmp. it has to be used $('.hasint').mouseleave)
Ondrej
A: 

could it be, that your tooltip-div is overlaying your trigger-div? In that case the appearing tooltip-div triggers the mouseleave of the trigger-div.. At least that's what happend to me some divs ago. I solved that problem by overlaying everything with an invisible trigger-div - not so beautiful, but worked for me..

tillinberlin
Oh man, you're right! That's the problem. Ok, thx for solution, but I guess It would be better for my case to *somehow* make some condition like (if !ishover(el1) or !ishover(el2) ) el2.hide()
Ondrej
Glad to hear that..:]
tillinberlin