views:

388

answers:

2

On mouseover can this div be attached to the mouse pointer so that its contents are shown on mouseover?

<div id='show' style='display:none;'></div>

If so how is this done?

A: 

Try this:

<div id='show' onmouseover="this.style.display = 'block';"></div>

But for that to work, the div should be visible first. However, if the div is hiddne (display:none) then onmoueever event won't be able to find the div and no event will be triggered on it. Having said that, try this which uses visibility property.

<div id='show' onmouseover="this.style.visibility = 'visible';"  onmouseout="this.style.visibility = 'hidden';"></div>
Sarfraz
A: 
<div onmousemove="position();" onmouseout="hide();">abc</div>
<div id="tip" style="position: fixed; visibility: hidden;">that's abc!</div>
<script type="text/javascript">
  function position() {
    var d = document.getElementById('tip');
    d.style.visibility = 'visible';
    d.style.left = event.screenX + 'px';
    d.style.top = event.screenY + 'px';
  }
  function hide() {
    document.getElementById('tip').style.visibility = 'hidden';
  }
</script>

When the user mouses over the "abc" div, the "that's abc!" div is shown next to the mouse cursor (and follows it).

Max Shawabkeh
k. Can u tell me whtas wrong with this code..var ele=document.getElementById('show'); document.getElementById('show').innerHTML = el.innerHTML; ele.width='200px'; ele.height='30px'; ele.bgcolor='#a9a9a9'; ele.color='#fff'; ele.position='absolute'; ele.display='block'; $(window).mouseover(function(event) { $("#show").css({'top': event.pageY, 'left': event.pageX}); $('#show').height(); });
Hulk
event.screenX + 'py' should be event.screenY + 'px'
Mic
@Hulk: The problem with that code is that it's unformatted and impossible to read. More seriously, though, `el` is not defined and you're mixing jQuery and pure javascript code - do you have jQuery loaded? Also, the `height()` call does nothing. @Mic: Thanks; fixed.
Max Shawabkeh
yes the jquery and javascript are loaded up to the page
Hulk
Shouldn't you use ele.style.width, ele.style.height, etc.? And Hulk, please make a new question for this.
Marcel Korpel