Toni Michel Caubet
Toni Michel Caubet

Reputation: 20163

Making my own tooltip jquery plugin - relative to mouse, how to know where the mouse is?

ok, basically i made 2 functions: show tooltip and hide tooltip and it can get ajax config or simple config, i just show you:

    <style type="text/css">

    .nube{display:none; height:auto; width:auto; position:absolute; z-index:99; left:200px;
          background-color:#fff;
          color:#999; border:1px solid #EEE; padding:20px; padding-top:10px; padding-bottom:10px;
-moz-border-radius-bottomleft: 0em;
    -webkit-border-bottom-left-radius: 0em;
-moz-border-radius-bottomright: 0.8em;
    -webkit-border-bottom-right-radius: 0.8em;
-moz-border-radius-topright: 0.8em;
    -webkit-border-top-right-radius: 0.8em;
-moz-border-radius-topleft: 0.8em;
    -webkit-border-top-left-radius: 0.8em;
}

</style>

And this is the Script

<script type="text/javascript">

function mostrar_tooltip(div,contenido,ajax){
    /// for now this 'if' is returns false allways so the last line its executed only
    if(contenido != ''){
    if(ajax == 0){
        // entonces no hay que
        $('#'+div).load('#'+contenido);
    }else{
        // contenido contiene la palabra clave a enviar a router
        $('#'+div).load('./router.php?que=tooltip&clave='+contenido);
    }
    // ya tenemos el contenido en el div (esta oculto y posicionado con CSS
    }
    $('#'+div).css({position:'fixed', top:($(window).scrollTop()+200)+"px", left:"200px"})
    $('#'+div).fadeIn(300);

}

function ocultar_tooltip(div){
    $('#'+div).fadeOut(100);
}


</script>

html looks like

 <span class="nube" id="tip"> TIP HERE </span>

<p onmouseover="mostrar_tooltip(tip,'',0)" onmouseout="ocultar_tooltip(tip)"> HOVER ME </p>

It works fine, but the problem it's that i want it relative to the mouse, not to the scroll...

I would like the tooltip to be relative to the mouse position, can i do that?

i know has something to do with

  .css({top:evt.pageY, left:evt.pageX})

and

function(evt){}

but i don't know how to combine it... :S thank you!

Upvotes: 0

Views: 812

Answers (1)

Neil
Neil

Reputation: 3231

Tony,

Here is some very simple tooltip code from javascript on my own website. It positions the tooltip relative to the cursor. Perhaps you can modify yours accordingly.

$(slide).hover(
  function(event){
    $('#tooltip').css({position: 'absolute', top: (event.pageY - 10), left: (event.pageX + 20)}).show("fast");      
  },
  function(){
     $(tooltip).hide("fast");
  }
 );

The tooltip itself is:

<div id='tooltip'>Right-click to download</div>

The css for it is:

#tooltip{
display: block;
position: absolute;
z-index: 9999;
border: 2px solid: #000;
background: #fff;
color: #000;
padding: 3px 6px;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;

}

Regards Neil

Upvotes: 1

Related Questions