Hiding the mouse cursor when idle using JavaScript

后端 未结 7 1816
灰色年华
灰色年华 2021-02-14 01:29

Is it possible to use JavaScript to set the cursor attribute to the property none if the mouse is inactive for a certain amount of time (say, five seco

7条回答
  •  执笔经年
    2021-02-14 02:30

    This worked for me (taken from https://gist.github.com/josephwegner/1228975).

    Note reference to an html element with id wrapper.

    //Requires jQuery - http://code.jquery.com/jquery-1.6.4.min.js
    $(document).ready(function() { 
    
    
        var idleMouseTimer;
        var forceMouseHide = false;
    
        $("body").css('cursor', 'none');
    
        $("#wrapper").mousemove(function(ev) {
                if(!forceMouseHide) {
                        $("body").css('cursor', '');
    
                        clearTimeout(idleMouseTimer);
    
                        idleMouseTimer = setTimeout(function() {
                                $("body").css('cursor', 'none');
    
                                forceMouseHide = true;
                                setTimeout(function() {
                                        forceMouseHide = false;
                                }, 200);
                        }, 1000);
                }
        });
    });
    

提交回复
热议问题