10

I'm currently working on a project that involves elements where a custom mouse pointer will be used. The code that I'm using for that function is simple just:

.hand {
    cursor:url(/images/cursor1.gif);
}

Now to my question: - I was just wondering (as the title says) if there are anyway that I can make the cursor change to cursor2.gif when you click inside the div-class "hand" (?).

3
  • Are you using plain JavaScript or jQuery to handle this click? Commented Mar 28, 2013 at 22:05
  • Do you want the cursor to change only while the mouse button is down, or do you want it to change (semi-) permanently after the user clicks? Commented Mar 28, 2013 at 22:11
  • @Adrian - semi, but it should work as well as long the mouse is down. Commented Mar 28, 2013 at 22:48

3 Answers 3

8

Use the :active psuedo-class:

.hand:active {
    cursor: url('/imgages/cursor1.gif');
}
Sign up to request clarification or add additional context in comments.

2 Comments

It seems to work, but the only problem is that as soon as I click inside .hand cursor2 appears, and disappears just as quick. Here's my CSS: .hand { width: 1500px; height: 500px; background: pink; cursor:url(/images/cursor1.gif), none; } .hand:active { cursor: url(/images/cursor2.gif), none; }
There's a minor typo: 'imgages'.
0

Try this jquery

(function(){
$hand = $('.hand');
$hand.click(function() {
    $hand.css('cursor','url(YOURGIF.GIF)');
});
});

Comments

0

I gather the difficulty is that you need the cursor to stayed changed after you have clicked, not just while the button is depressed.

Does this work for you?

function toggle(){

    var body = document.body; 
    body.id = ( body.id ) ? body.id : 'body_id'; // ffox

    body.use_custom_cursor = !body.use_custom_cursor;

    body.style.cursor = body.use_custom_cursor ? 'url(/images/cursor1.gif)' : 'auto';

}

and then...

<div onclick="toggle()">Target</div>

I've only tested with Webkit and Firefox, not sure if IE likes it.

1 Comment

The 'ffox' line is required to override a default CSS cursor rule that Mozilla use: "body:not([id])". I just give the body an id if it doesn't already have one. It doesn't need to be 'body_id', it can be anything.

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.