Search code examples
javascriptonmousemoveballoon

How to Show Ballon tooltip when mouse stops


[edit] So I used one of the javascript tooltips suggested below. I got the tips to show when you stop and hide if you move. The only problem is it works when I do this:

document.onmousemove = (function() {
    var onmousestop = function() {
        Tip('Click to search here');
        document.getElementById('MyDiv').onmousemove = function() {
        UnTip();
    };
    }, thread;

    return function() {
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

But I want the function to only apply to a specific div and if I change the first line to "document.getElementById('MyDiv').onmousemove = (function() {" I get a javascript error document.getElementById('MyDiv') is null What am I missing....??

[/edit]

I want to display a balloon style message when the users mouse stops on an element from more than say 1.5 seconds. And then if they move the mouse I would like to hide the balloon. I am trying to use some JavaScript code I found posted out in the wild. Here is the code I am using to detect when the mouse has stopped:

document.onmousemove = (function() {
    var onmousestop = function() {
        //code to show the ballon
        };
    }, thread;

    return function() {
        clearTimeout(thread);
        thread = setTimeout(onmousestop, 1500);
    };
})();

So I have two questions. One, does anyone have a recommended lightweight javascript balloon that will display at the cursor location. And two, the detect mouse stopped code works ok but I am stumped on how to detect that the mouse has started moving again and hide the balloon. Thanks...


Solution

  • A bit late to be answering this, but this will be helpful for those in need.

    I needed this function to be able to detect when the mouse stopped moving for a certain time to hide an HTML/JS player controller when hovering over a video. This is the revised code for the tooltip:

    document.getElementById('MyDiv').onmousemove = (function() {
        var onmousestop = function() {
            Tip('Click to search here');
        }, thread;
    
        return function() {
            UnTip();
            clearTimeout(thread);
            thread = setTimeout(onmousestop, 1500);
        };
    })();
    

    In my case, I used a bit of jQuery for selecting the elements for my player controller:

    $('div.video')[0].onmousemove = (function() {
        var onmousestop = function() {
            $('div.controls').fadeOut('fast');
        }, thread;
    
        return function() {
            $('div.controls').fadeIn('fast');
            clearTimeout(thread);
            thread = setTimeout(onmousestop, 1500);
        };
    })();