34
votes

I have a link that, when clicked, I would like it to move the position of the mouse to the right (or anywhere within the viewport, for that matter).

in code it would probably look similar to the following:

$('a#expand').click(function(e){
    $(document)
       .mouseXPos(e.pageX + 50)
       .mouseYPos(e.pageY + 50);
});

Chaining might not be necessary, of course, but a similar 'set mouse position' functionality is what I am after.

I've seen solutions to move the cursor position to a certain spot in the text, but I didn't glean much from them.

6
This sounds like something that will really irritate users! I hope you have a good reason for doing this, not that it's possible. - ScottE
I should explain further. I'm using Brian's great hoverIntent plugin for jQuery, along with a menu that includes a linked tab that expands the menu. I'm animating the sliding out, but the hoverIntent misfires the hover off if the mouse stays still while the menu is animating. Sounds odd but this plugin catches all of the rest of the classic mouseenter/leave misfires that devs typically run across. He knows about this issue and is working on it for the next release. Being able to somehow move the mouse one pixel over would be great. Either client-side or server-side solution would be awesome. - Michael
while this sounds at first blush like something that would irritate users, let me describe a situation where not moving the mouse would irritate users and moving it might actually please them... imagine that you click something on a web page and the elements on the page rearrange; if the mouse has not moved, you are no longer over the element on which you clicked; the mouse has not moved relative to the page but it has moved relative to the content. "moving" the mouse relative to the page could be required in order to provide the experience that the mouse has not moved. - David Alpert

6 Answers

57
votes

There is no mechanism for moving the mouse via JavaScript.

11
votes

I may be wrong, but I don't think it's possible to move the mouse pointer from client-side script. Given the potential for abuse, I certainly hope it isn't.

4
votes

There's no way to accomplish mouse position change via JavaScript or any Client-Side Script. The only reason for that is not to give a client side script potential for abuse as stated before.

4
votes

You could hide the cursor, and show another one at a different place.

Good to have when moving around in a maze for example. The cursor looks like it's stopped but you will see it again when you move outside the window.

2
votes

As other users already have mentioned, there isn't any mechanism is Javascript to do that. However, you can disable the mouse and implement a cursor to do what you need. Here is a link that explains how. How to implement a custom cursor.

-1
votes

You map change scroll position that will automatically move your pointer to required position;

$(document).scrollTop();

For some case, I was required to stay pointer on same checkbox although a button show/hide was causing a bubbling... so I did something like;

$(document).scrollTop( $(document).scrollTop() + parseInt($('.btn-show-selected-group').outerHeight()) );
$(document).scrollTop( $(document).scrollTop() - parseInt($('.btn-show-selected-group').outerHeight()) );