David19801
David19801

Reputation: 11448

Mouse pointer with text "tooltip"

I'm using HTML/jquery/CSS and have a piece of text - "test".

How can I make the "test" text follow the mouse pointer (or replace the mouse pointer icon with the text if that is easier)?

EDIT From Tims answer:

CSS:   #follower { background: #fff; padding: 5px; border: 1px solid #ddd; position: absolute; }


JS: $(document).ready(function(){
    $("#follower").hide();
    $(document).mousemove(function(e){
        $("#follower").show();
        $("#follower").css({
            top: (e.pageY + 15) + "px",
            left: (e.pageX + 15) + "px"
        });
    });
});

Upvotes: 0

Views: 2479

Answers (2)

Ashfaq Shaikh
Ashfaq Shaikh

Reputation: 1668

See it in action at this JsFiddle

<div id="demo-mouse" class="box">
    <h3>Hover over this box to see mouse tracking in action</h3>
</div>

$(document).ready(function()
{
    $('#demo-mouse').qtip({
        content: 'I position myself at the current mouse position',
        position: {
            my: 'top left',
            target: 'mouse',
            viewport: $(window), // Keep it on-screen at all times if possible
            adjust: {
                x: 10,  y: 10
            }
        },
        hide: {
            fixed: true // Helps to prevent the tooltip from hiding ocassionally when tracking!
        },
        style: 'ui-tooltip-shadow'
    });
});

Upvotes: 0

user900202
user900202

Reputation:

you can use the mousemove event to catch the position of the mouse, and then set to the text span.

The Html:

<html>
<body onmousemove="mousemove()">
<span id="textSpan" style="position:absolute">test</span>
</body>
</html>

the js:

function mousemove()
{
$('#textSpan').css({
    'top' : event.pageY,
    'left' : event.pageX
}); 
}

Upvotes: 1

Related Questions