views:

71

answers:

1

I would like to change the cursor from default to pointer when the mouse enter the rectangle (50, 50, 100, 100) of the body element (the numbers are in pixels).

I know that I could define a div, place it at this position, and set cursor: pointer; on it, but I'm looking for some way without defining a div.

I would like to something like:

if (mousePosition inside rectangle) {
    change cursor to pointer
} else {
    change cursor to default
}

Is that possible to do that ?

+1  A: 

Try something like this (untested though):

$("body").mousemove(function(e){
    if (e.pageX > 50 && e.pageY > 50 && e.pageX < 100 && e.pageY < 100)
        $("body").css("cursor", "pointer");
    else
        $("body").css("cursor", "default");
});

You might want to use a global variable though instead of just appling the properties each time for performance reasons. You also might want to look into using CSS classes instead of directly setting the style in JS.

Update: Why did I think style instead of css... :(

MiffTheFox
instead of .style("cursor", "default") I typically use .style("cursor", "") in case the "default" cursor wasn't actually used on the page.
Sparafusile
`.style() won't work, regardless how you try to call it, because it's no jQuery method. You are looking for `.css()`
jAndy
You should use `.css()` instead of `.style()` here, though it doesn't ensure this will work. For example if you're on top of an element that has a cursor defined, that's a more-local style than `<body>` has...but this should work well enough.
Nick Craver
My main problem is with `<input type='file' />` element which enforces the cursor to be something else. See here: http://jsfiddle.net/rdnhh/1/. Any ideas for workaround ?
Misha Moroshko
@jAndy @Nick -- \*facepalms at his own stupidity\*
MiffTheFox