I need a way of changing the mouse-cursor on a html-page. I know this can be done with css, but I need to be able to change it at runtime, like for instance having buttons on the page, and when they're clicked they change the cursor to a specific custom graphic. I think the best (or only?) way of doing this is through javascript? I hope there's a way of doing this nicely that will work on all of the major browsers. I would be very grateful if someone could help me with this.
Thanks in advance
The default cursor for a hyperlink is "pointer". To change it, you need to specify the cursor type for your <a> element with the CSS :hover selector. In our example, we style only the "link" class.
You can simply use the CSS cursor property with the value pointer to change the cursor into a hand pointer while hover over any element and not just hyperlink. In the following example when you place the cursor over the list item, it will change into a hand pointer instead of the default text selection cursor.
Thanks for the replies. I finally got it working. Here's how I did it:
<html>
<head>
<script type="text/javascript">
function changeToCursor1(){
document.body.style.cursor="url('cursor1.ani'),url('cursor1.cur'), default";
}
function changeToCursor2(){
document.body.style.cursor="url('cursor2.ani'),url('cursor2.cur'), default";
}
</script>
</head>
<body>
<form>
<input type="button" value="Change to cursor 1" onclick="changeToCursor1()" /><br>
<input type="button" value="Change to cursor 2" onclick="changeToCursor2()" />
</form>
</body>
I found out that to get it to work in Firefox you must pass at least 2 choices of cursors, e.g. cursor="url('cursor1.cur'), default" Or else it wont work. Also, in Firefox it doesn't work with ani-cursors, only cur. Which is why I've put a cur after ani. The ani will show up in IE, the cur in Firefox.
Does anyone know if it's possible to change the cursor in IE without the active-X warning showing up and the user having to accept?
http://www.javascriptkit.com/dhtmltutors/csscursors.shtml
Theres an example at the bottom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With