Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force the mouse cursor to change without a mouse movement in Javascript?

In my webpage, testing on Chrome, I have a button div. The button is styled so that it has a hover state in a different colour, and a hand-shaped mouse pointer. All this is fine.

When the button is pressed, it triggers an animation, and I don't want to let the user press the button again until it's done, so I put a semi-opaque div over the top to block the button.

The problem comes when the animation completes and the div is removed. The mouse pointer is over the button but the hover state isn't active until the user actually moves the mouse, then the mouse pointer changes and all is well.

Note that the click still works - this is a purely cosmetic (but annoying) aberration.

Can I force the browser to re-evaluate the point under the cursor?

like image 326
izb Avatar asked Jan 03 '12 23:01

izb


People also ask

How do I change my mouse cursor to anything?

To give all of your pointers a new look, click the Scheme drop-down list, and then click a new mouse pointer scheme. To change an individual pointer, under Customize, click the pointer you want to change in the list, click Browse, click the pointer you want to use, and then click Open.

Can you move the cursor with Javascript?

You cannot move the actual mouse pointer in Javascript. You can, however move a pointer shaped image and pretend that you can. :-) Better yet, you can move a cat image around, following the mouse cursor, and try to use it to chase the cursor into the position you want.

How do I change my cursor behavior?

In the Mouse Properties box, click on the Pointers and Pointer Options tab, and adjust the options to change the shape and size of your cursor by changing the “scheme”. In the “Pointer Options” tab, you can change the speed, visibility, and other characteristics of your pointer.

How do I permanently change my custom cursor?

Step 1: Navigate to the Mouse properties window as we did earlier. Step 2: Select the Pointers tab. Step 3: To select a custom cursor for the highlighted individual icon, click Browse. Step 4: That will open the default cursors folder, where hundreds of different cursor options are available.


2 Answers

The correct way to prevent input to a button is to disable it. You can toggle the cursor style with the CSS cursor property.

Javascript:

var theButton = document.getElementById('theButton');
    theButton.disabled = true;
    theButton.setAttribute('style','cursor:default;');

// animation is complete

theButton.disabled = false;
theButton.removeAttribute('style');

jQuery:

var $theButton = $('#theButton').prop('disabled',true).css('cursor','default');

// animation is complete

$theButton.prop('disabled',false).css('cursor','pointer');
like image 82
Ansel Santosa Avatar answered Sep 21 '22 21:09

Ansel Santosa


Check the position of the mouse when the animation ends and you remove the div, or just always store them and check that value when it ends to see if the cursor is still over your button. You could do this with event.clientX, event.clientY or event.pageX, event.pageY something similar to those(not completely sure just did some quick research but those seemed to work in chrome,IE, and firefox). Then if the mouse is still over the button, trigger the on.hover for the button element again.

like image 25
Ryan Avatar answered Sep 18 '22 21:09

Ryan