Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show DIV at mouse cursor on hover of span

I want a DIV to unhide and appear at the mouse cursor when the user hovers over a SPAN or DIV.

I made this function but it does not work (jquery is loaded).

function ShowHoverDiv(divid){

    function(e){
        var left  = clientX  + "px";
        var top  = clientY  + "px";
        $("#"+divid).toggle(150).css("top",top).css("left",left).css("position","fixed");
        return false;
    }


}

<div id="divtoshow" style="display:none">test</div>
<br><br>
<span onmouseover="ShowHoverDIV('divtoshow')">Mouse over this</span>
like image 693
Mbrouwer88 Avatar asked Mar 01 '13 12:03

Mbrouwer88


People also ask

Can we hover with mouse?

In computing, a mouseover , mouse hover or hover box is a graphical control element that is activated when the user moves or hovers the pointer over a trigger area, usually with a mouse, but also possible with a digital pen. Mouseover control elements are common in web browsers.

What is displayed adjacent to the mouse pointer when the pointer is hovering over a text item?

On desktop, it is used in conjunction with a cursor, usually a pointer, whereby the tooltip appears when a user hovers the pointer over an item without clicking it.

What is on mouse hover?

The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children. Tip: This event is often used together with the onmouseout event, which occurs when a user moves the mouse pointer out of an element.


2 Answers

You're pretty much there:

function hoverdiv(e,divid){

    var left  = e.clientX  + "px";
    var top  = e.clientY  + "px";

    var div = document.getElementById(divid);

    div.style.left = left;
    div.style.top = top;

    $("#"+divid).toggle();
    return false;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divtoshow" style="position: fixed;display:none;">test</div>
    <br><br>
    <span onmouseover="hoverdiv(event,'divtoshow')" onmouseout="hoverdiv(event,'divtoshow')">Mouse over this</span>
like image 199
Simon Adcock Avatar answered Sep 30 '22 12:09

Simon Adcock


i quickly set up this example, starting with Dušan Radojević code:

$("#spanhovering").hover(function(event) {
    $("#divtoshow").css({top: event.clientY, left: event.clientX}).show();
}, function() {
    $("#divtoshow").hide();
});

jsfiddle

like image 38
Imperative Avatar answered Sep 30 '22 12:09

Imperative