Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery hide mouse if it's not moving

I'm trying to hide the mouse if it hasn't moved for a period of time.

This is the code I'm using:

$(document).ready(function() {
    var j;
    $(document).mousemove(function() {
        clearTimeout(j);
        $('html').css({cursor: 'default'});
        j = setTimeout('hide();', 1000);
    });
});

function hide() {
    $('html').css({cursor: 'none'});
}

When the hide() function is called the cursor is hidden, but unhides a split second later. Any help is appreciated.

like image 222
Clinton Jooooones Avatar asked May 12 '12 14:05

Clinton Jooooones


People also ask

How do I hide my cursor when not moving?

You will see a “Mouse Properties” window. At the top of this window, click the “Pointer Options” tab. The “Pointer Options” tab displays various mouse settings. Here, in the “Visibility” section, enable the “Hide Pointer While Typing” option.

What does hide () do in jQuery?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

Can you move mouse with Javascript?

You can't move the mouse pointer using javascript, and thus for obvious security reasons. The best way to achieve this effect would be to actually place the control under the mouse pointer.


1 Answers

Your initial problem is that the hiding of the mouse triggers mousemove and thus immediately resets it back to default. So you could solve that like this...

var justHidden = false;

$(document).ready(function() {
    var j;
    $(document).mousemove(function() {
        if (!justHidden) {
            justHidden = false;
            console.log('move');
            clearTimeout(j);
            $('html').css({cursor: 'default'});
            j = setTimeout('hide();', 1000);
        }
    });
});

function hide() {
    $('html').css({cursor: 'none'});
    justHidden = true;
}​

...BUUUUUT...

You face a problem here which at the moment seems unsolvable to me. That is, a hidden mouse does not trigger mousemove ever, so once it's hidden you will not be able to unhide it as far as I can tell.

I'll keep investigating to see if there's a solution I'm missing.

like image 195
mVChr Avatar answered Sep 30 '22 10:09

mVChr