Questions & Answers

Change Mouse Cursor for whole page?

I have a drag UI program, where the mouse cursor on the draggable element changes to a grabbing hand on click.

The problem is, I allow the drag to happen anywhere on screen, and the cursor changes over anchor tags, etc...

I've tried $('*').addClass('grabbing'), but it's REALLY expensive.

Is there a simple easy code efficient way to handle this?

2023-01-07 20:32:53
Answers(5) :

Do it at the CSS level:

* {
   cursor: pointer;

doing it in jquery forces it to iterate every single node in the dom and rewrite its css classes. On a long document, that's a huge waste of time.

2023-01-07 20:32:53
But this will always be active. The cursor needs to change only while the mouse is down. It needs to happen in Javascript somehow.
2023-01-07 20:32:53
You can dynamically add/remove css rules via javascript. Just do it from within the onmousedown/onmouseup and it should be quicker than adding/removing classes from every dom node on the page.
2023-01-07 20:32:53
@Mark B Cool. Can you provide a new answer with an example? I'll give it a shot and if it works right I'll mark it.
2023-01-07 20:32:53
Same problem as the other answer - Other CSS elements and classes will override this if they specify a cursor.
2023-01-07 20:32:53
@mike: there's always !important.
2023-01-07 20:32:53
@MarcB It seems I can add but not remove with this. If you figure out the code and post it as an answer I'll mark it. There is a JQuery Rule plugin that might work.
2023-01-07 20:32:53
@Wesley: .cssRules and .rules are just arrays. you can use regular array operations to remove a particular rule by deleting its corresponding array element.
2023-01-07 20:32:53
Can add as a subclass with !important to preserve without dynamically adding rules. body.loading-cursor *{ cursor: wait !important; }
2023-01-07 20:32:53
still doesn't set the cursor for the whole page when BODY is shorter than 100%

try this






If you firebug is on, you can see the changes in body style tag. But some how it's not working. You can take it as a reference and try similar approach the get solution.

2023-01-07 20:32:53
Won't hire zindex elements override that?
2023-01-07 20:32:53
@MikeChristensen Correct. This won't quite do it.

When you need it, you can invoke a single line of javascript:'cursor', 'pointer');

When you want to undo it again, you can execute another single line:


I tried most of these, but not all elements will display the cursor. The quick and dirty method is to iterate the entire dom and set the cursor for each element.

document.querySelectorAll('*').forEach(function(node) { = 'progress';

Execute ajax or whatever here....Followed by:

document.querySelectorAll('*').forEach(function(node) { = 'default';

Though this is not a direct answer to this question but I want to draw attentions to that we can solve the problem differently.

By putting an empty fullscreen div on top of everything except drop zones, activate it after mousedown event. So we can simply change cursor for entire screen through :hover class of that div .