Mouse pointer with text “tooltip”

两盒软妹~` 提交于 2021-01-29 06:15:14

问题


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"
        });
    });
});

回答1:


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
}); 
}



回答2:


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'
    });
});


来源:https://stackoverflow.com/questions/9078576/mouse-pointer-with-text-tooltip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!