Wait cursor over entire html page

后端 未结 14 1304
离开以前
离开以前 2020-12-02 14:19

Is it possible to set the cursor to \'wait\' on the entire html page in a simple way? The idea is to show the user that something is going on while an ajax call is being com

相关标签:
14条回答
  • 2020-12-02 14:38

    I have been struggling with this problem for hours today. Basically everything was working just fine in FireFox but (of course) not in IE. In IE the wait cursor was showing AFTER the time consuming function was executed.

    I finally found the trick on this site: http://www.codingforums.com/archive/index.php/t-37185.html

    Code:

    //...
    document.body.style.cursor = 'wait';
    setTimeout(this.SomeLongFunction, 1);
    
    //setTimeout syntax when calling a function with parameters
    //setTimeout(function() {MyClass.SomeLongFunction(someParam);}, 1);
    
    //no () after function name this is a function ref not a function call
    setTimeout(this.SetDefaultCursor, 1);
    ...
    
    function SetDefaultCursor() {document.body.style.cursor = 'default';}
    
    function SomeLongFunction(someParam) {...}
    

    My code runs in a JavaScript class hence the this and MyClass (MyClass is a singleton).

    I had the same problems when trying to display a div as described on this page. In IE it was showing after the function had been executed. So I guess this trick would solve that problem too.

    Thanks a zillion time to glenngv the author of the post. You really made my day!!!

    0 讨论(0)
  • 2020-12-02 14:38

    My Two pence:

    Step 1: Declare an array. This will be used to store the original cursors that were assigned:

    var vArrOriginalCursors = new Array(2);
    

    Step 2: Implement the function cursorModifyEntirePage

     function CursorModifyEntirePage(CursorType){
        var elements = document.body.getElementsByTagName('*');
        alert("These are the elements found:" + elements.length);
        let lclCntr = 0;
        vArrOriginalCursors.length = elements.length; 
        for(lclCntr = 0; lclCntr < elements.length; lclCntr++){
            vArrOriginalCursors[lclCntr] = elements[lclCntr].style.cursor;
            elements[lclCntr].style.cursor = CursorType;
        }
    }
    

    What it does: Gets all the elements on the page. Stores the original cursors assigned to them in the array declared in step 1. Modifies the cursors to the desired cursor as passed by parameter CursorType

    Step 3: Restore the cursors on the page

     function CursorRestoreEntirePage(){
        let lclCntr = 0;
        var elements = document.body.getElementsByTagName('*');
        for(lclCntr = 0; lclCntr < elements.length; lclCntr++){
            elements[lclCntr].style.cursor = vArrOriginalCursors[lclCntr];
        }
    }
    

    I have run this in an application and it works fine. Only caveat is that I have not tested it when you are dynamically adding the elements.

    0 讨论(0)
  • 2020-12-02 14:43

    Why don't you just use one of those fancy loading graphics (eg: http://ajaxload.info/)? The waiting cursor is for the browser itself - so whenever it appears it has something to do with the browser and not with the page.

    0 讨论(0)
  • 2020-12-02 14:43

    This pure JavaScript seems to work pretty well ... tested on FireFox, Chrome, and Edge browsers.

    I'm not sure about the performance of this if you had an overabundance of elements on your page and a slow computer ... try it and see.

    Set cursor for all elements to wait:

    Object.values(document.querySelectorAll('*')).forEach(element => element.style.cursor = "wait");
    

    Set cursor for all elements back to default:

    Object.values(document.querySelectorAll('*')).forEach(element => element.style.cursor = "default");
    

    An alternative (and perhaps a bit more readable) version would be to create a setCursor function as follows:

    function setCursor(cursor)
    {
        var x = document.querySelectorAll("*");
    
        for (var i = 0; i < x.length; i++)
        {
            x[i].style.cursor = cursor;
        }
    }
    

    and then call

    setCursor("wait");
    

    and

    setCursor("default");
    

    to set the wait cursor and default cursor respectively.

    0 讨论(0)
  • 2020-12-02 14:45

    BlockUI is the answer for everything. Give it a try.

    http://www.malsup.com/jquery/block/

    0 讨论(0)
  • 2020-12-02 14:46

    To set the cursor from JavaScript for the whole window, use:

    document.documentElement.style.cursor = 'wait';
    

    From CSS:

    html { cursor: wait; }
    

    Add further logic as needed.

    0 讨论(0)
提交回复
热议问题