0

I am working on a browser game where I am using custom cursors. I am setting up the custom cursor for the entire page in my CSS (for some reason if I set it up for the 'body' sometimes it changes back to default cursor at some area)

html {
  cursor: url("http://www.iwansfactory.com/tycoon/defaultcursor.png"), default;
}

I would like to overwrite the url property of the above CSS using javascript

For example when the user is moving the map I would like to use this custom cursor:

http://www.iwansfactory.com/tycoon/movecursor.png

How can I change this value using javascript? Thanks in advance! :)

2
  • 1
    document.documentElement.style.cursor = 'url("http://www.iwansfactory.com/tycoon/movecursor.png"), default'; Commented Apr 6, 2016 at 5:25
  • Thanks mate, this is exactly what I was looking for! Commented Apr 6, 2016 at 5:30

5 Answers 5

3
document.documentElement.style.cursor = 'url("http://www.iwansfactory.com/tycoon/movecursor.png"), default';
Sign up to request clarification or add additional context in comments.

Comments

1

The way you explain it, you don't even need javascript ? Why not just do something like

.map:hover{ cursor : url('http://www.iwansfactory.com/tycoon/movecursor.png') }

2 Comments

I will use a few different cursor (for moving on map, building a road, planting a tree, etc.) sorry for didn't explain it further
Ho then you want it to change on click over something I guess ?
0

You can try this, If you need js, otherwise follow the above css...

      <span onmouseover="changeCursor(this,'http://www.iwansfactory.com/tycoon/movecursor.png');" onmouseout="changeCursor(this,' url("http://www.iwansfactory.com/tycoon/defaultcursor.png"), default');"> Hello World </span>

        function changeCursor(el, cursor)
        {   

             el.style.cursor = cursor;
         }

Comments

0

if you want the cursor changes to the move-cursor, you could try the event MouseOver to change its style when the mouse hover on the map,and the event MouseOut to return to default.

Comments

0

You could add css classes for each cursor type, then onmouseenter event apply your game logic to choosing which css class to apply.

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.