0

I know we can use HTMLElement.style.cursor to change a cursor when hovering an element. But can we change the cursor using javascript without the need of mouse hovering anything? Thanks.

This is for a game app on the web/web view

I need a solution that does not depend on the dom

5
  • stackoverflow.com/questions/17414034/… Commented Nov 26, 2014 at 0:38
  • @KimGysen Obviously its a different question Commented Nov 26, 2014 at 0:39
  • @KimGysen the cursor would be dependent on the dom. for example, if you change the cursor on the current element you are hovering over, you move your mouse and your cursor changes Commented Nov 26, 2014 at 0:47
  • Yes, but your scripts interact with the browser through the browser's DOM api... I don't think that there is another way to do it afaik, and I don't really think that there is a need to. The proposed solutions should work? Commented Nov 26, 2014 at 0:51
  • hence the question... If there is any api/library to interact with the mouse directly it would be great. Commented Nov 26, 2014 at 0:55

3 Answers 3

2

change the cursor for all elements: http://jsfiddle.net/m2rLy13L/1/

$('*').css('cursor','help');
Sign up to request clarification or add additional context in comments.

Comments

1

you can use a div that will wrap the entire document: http://jsfiddle.net/ymzrocks/un8b6rbc/

html:

<div class="frame" style="background:#ababab"></div>
<div class="frame">
content here
</div>

css:

.frame
{
    position:fixed;
    top:0;
    left:0;
    right:0;
    bottom:0;
    ;
}

js:

$('div.frame').css('cursor', 'help');

5 Comments

Thanks, I prefer not to use a div wrapper as I need to click on the html. I would like to use something like mouse.cursor('crosshair') so its easier and cleaner.
try fiddle - the wrapper is at the background not at the front.
Thanks, but it breaks when there is something in front... jsfiddle.net/m2rLy13L
i just watch you fiddle and it works. console.log prints to the developer console not to the screen. it is just fine if you don't like my solution, but when you say that it breaks it may confuse other users
Sorry, I meant the mouse cursor changes in front of the button. the button itself works fine.
0

Seems https://www.npmjs.org/package/pointer-cursor might be a good package to use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.