Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing cursor to waiting in javascript/jquery

enter image description here

How would i get my cursor to change to this loading icon when a function is called and how would i change it back to a normal cursor in javascript/jquery

like image 948
ahmet Avatar asked Mar 13 '12 09:03

ahmet


People also ask

How do I change my cursor in WPF?

A switch statement filters on the cursor name and sets the Cursor property on the Border which is named DisplayArea. If the cursor change is set to "Entire Application", the OverrideCursor property is set to the Cursor property of the Border control. This forces the cursor to change for the whole application.


12 Answers

In your jQuery use:

$("body").css("cursor", "progress");

and then back to normal again

$("body").css("cursor", "default");
like image 56
Stanley Avatar answered Oct 01 '22 15:10

Stanley


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.

like image 25
hrabinowitz Avatar answered Oct 01 '22 14:10

hrabinowitz


Please don't use jQuery for this in 2021! There is no reason to include an entire external library just to perform this one action which can be achieved with one line:

Change cursor to spinner: document.body.style.cursor = 'wait'

Revert cursor to normal: document.body.style.cursor = 'default'

like image 24
kchuang7 Avatar answered Oct 01 '22 14:10

kchuang7


jQuery:
$("body").css("cursor", "progress");

back again
$("body").css("cursor", "default");

Pure:
document.body.style.cursor = 'progress';

back again
document.body.style.cursor = 'default';

like image 36
xoxn-- 1'w3k4n Avatar answered Oct 01 '22 14:10

xoxn-- 1'w3k4n


The following is my preferred way, and will change the cursor everytime a page is about to change i.e. beforeunload

$(window).on('beforeunload', function(){
   $('*').css("cursor", "progress");
});
like image 33
shaneparsons Avatar answered Oct 01 '22 14:10

shaneparsons


Using jquery and css :

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

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

CSS: .wait {cursor:wait}​

Demo here

like image 22
tusar Avatar answered Oct 01 '22 14:10

tusar


$('#some_id').click(function() {
  $("body").css("cursor", "progress");
  $.ajax({
    url: "test.html",
    context: document.body,
    success: function() {
      $("body").css("cursor", "default");
    }
  });
});

This will create a loading cursor till your ajax call succeeds.

like image 37
Kanishka Panamaldeniya Avatar answered Oct 01 '22 14:10

Kanishka Panamaldeniya


Override all single element

$("*").css("cursor", "progress");
like image 39
สุดเขต นะจ๊ะ Avatar answered Oct 01 '22 13:10

สุดเขต นะจ๊ะ


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

like image 30
Manse Avatar answered Oct 01 '22 15:10

Manse


Here is something else interesting you can do. Define a function to call just before each ajax call. Also assign a function to call after each ajax call is complete. The first function will set the wait cursor and the second will clear it. They look like the following:

$(document).ajaxComplete(function(event, request, settings) {
    $('*').css('cursor', 'default');
  });

function waitCursor() {
    $('*').css('cursor', 'progress');
  }
like image 22
Shawn Vincent Avatar answered Oct 01 '22 15:10

Shawn Vincent


If it saves too fast, try this:

<style media="screen" type="text/css">
    .autosave {display: inline; padding: 0 10px; color:green; font-weight: 400; font-style: italic;}
</style>

<input type="button" value="Save" onclick="save();" />&nbsp;
<span class="autosave" style="display: none;">Saved Successfully</span>


$('span.autosave').fadeIn("80");
$('span.autosave').delay("400");
$('span.autosave').fadeOut("80");
like image 27
Mark Eilenberger Avatar answered Oct 01 '22 15:10

Mark Eilenberger


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);
}
like image 38
cat_in_hat Avatar answered Oct 01 '22 13:10

cat_in_hat