Getting the browser cursor from “wait” to “auto” without the user moving the mouse

前端 未结 15 1996
自闭症患者
自闭症患者 2020-11-27 13:49

I use this jQuery code to set the mouse pointer to its busy state (hourglass) during an Ajax call...

$(\'body\').css(\'cursor\', \'wait\');

15条回答
  •  渐次进展
    2020-11-27 13:56

    First of all, you should be aware that if you have a cursor assigned to any tag within your body, $('body').css('cursor', 'wait'); will not change the cursor of that tag (like me, I use cursor: pointer; on all my anchor tag). You might want to look at my solution to this particular problem first : cursor wait for ajax call

    For the problem that the cursor is only updated once the user move the mouse on webkit browsers, as other people said, there is no real solution.

    That being said, there is still a workaround if you add a css spinner to the current cursor dynamically. This is not a perfect solution because you don't know for sure the size of the cursor and if the spinner will be correctly positioned.

    CSS spinner following the cursor: DEMO

    $.fn.extend(
    {
        reset_on : function(event_name, callback)
        { return this.off(event_name).on(event_name, callback); }
    });
    
    var g_loader = $('.loader');
    
    function add_cursor_progress(evt)
    {
        function refresh_pos(e_)
        {
            g_loader.css({
                display : "inline",
                left : e_.pageX + 8,
                top : e_.pageY - 8
            });
        }
        refresh_pos(evt);
        var id = ".addcursorprog"; // to avoid duplicate events
    
        $('html').reset_on('mousemove' + id, refresh_pos);
    
        $(window).
        reset_on('mouseenter' + id, function(){ g_loader.css('display', 'inline'); }).
        reset_on('mouseleave' + id, function(){ g_loader.css('display', 'none'); });
    }
    
    function remove_cursor_progress(evt)
    {
        var id = ".addcursorprog";
        g_loader.css('display', 'none');
    
        $('html').off('mousemove' + id);
        $(window).off('mouseenter' + id).off('mouseleave' + id);
    }
    
    $('.action').click(add_cursor_progress);
    $('.stop').click(remove_cursor_progress);
    

    You will need to check if it is a touch device as well var isTouchDevice = typeof window.ontouchstart !== 'undefined';

    In conclusion, you better try to add in your page a static spinner or something else that shows the loading process instead of trying to do it with the cursor.

提交回复
热议问题