Changing cursor to waiting in javascript/jquery

后端 未结 13 2038
孤街浪徒
孤街浪徒 2020-12-04 10:43

\"enter

How would i get my cursor to change to this loading icon when a function is ca

相关标签:
13条回答
  • 2020-12-04 11:12

    A colleague suggested an approach that I find preferable to the chosen solution here. First, in CSS, add this rule:

    body.waiting * {
        cursor: progress;
    }
    

    Then, to turn on the progress cursor, say:

    $('body').addClass('waiting');
    

    and to turn off the progress cursor, say:

    $('body').removeClass('waiting');
    

    The advantage of this approach is that when you turn off the progress cursor, whatever other cursors may have been defined in your CSS will be restored. If the CSS rule is not powerful enough in precedence to overrule other CSS rules, you can add an id to the body and to the rule, or use !important.

    0 讨论(0)
  • 2020-12-04 11:13

    I found that the only way to get the cursor to effectively reset its style back to what it had prior to being changed to the wait style was to set the original style in a style sheet or in style tags at the start of the page, set the class of the object in question to the name of the style. Then after your wait period, you set the cursor style back to an empty string, NOT "default" and it reverts back to its original value as set in your style tags or style sheet. Setting it to "default" after the wait period only changes the cursor style for every element to the style called "default" which is a pointer. It doesn't change it back to its former value.

    There are two conditions to make this work. First you must set the style in a style sheet or in the header of the page with style tags, NOT as an inline style and second is to reset its style by setting the wait style back to an empty string.

    0 讨论(0)
  • 2020-12-04 11:14

    Override all single element

    $("*").css("cursor", "progress");
    
    0 讨论(0)
  • 2020-12-04 11:17

    You don't need JavaScript for this. You can change the cursor to anything you want using CSS :

    selector {
        cursor: url(myimage.jpg), auto;
    }
    

    See here for browser support as there are some subtle differences depending on browser

    0 讨论(0)
  • 2020-12-04 11:19

    Setting the cursor for 'body' will change the cursor for the background of the page but not for controls on it. For example, buttons will still have the regular cursor when hovering over them. The following is what I am using:

    To set the 'wait' cursor, create a style element and insert in the head:

    var css = "* { cursor: wait; !important}";
    var style = document.createElement("style");
    style.type = "text/css";
    style.id = "mywaitcursorstyle";
    style.appendChild(document.createTextNode(css));
    document.head.appendChild(style);
    

    Then to restore the cursor, delete the style element:

    var style = document.getElementById("mywaitcursorstyle");
    if (style) {
      style.parentNode.removeChild(style);
    }
    
    0 讨论(0)
  • 2020-12-04 11:24

    Using jquery and css :

    $("#element").click(function(){ 
        $(this).addClass("wait");
    });​
    

    HTML: <div id="element">Click and wait</div>​

    CSS: .wait {cursor:wait}​

    Demo here

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