Cross-browser CSS Grab cursors for dragging

For a project I was looking at the best way to implement a grabbing hand cursor for dragging an element that would work well in all browsers.
After many google searches I have collated my findings below and hopefully it helps someone.

Firstly you’ll need some javascript to detect the current browser.
I use Browser Detect which works really well.

If it’s Firefox you can use the “-moz-grabbing” and “-moz-grab” cursors built in.

cursor: -moz-grab

For the other browsers you’ll need to load them from a URL.
You can download the cursors from here:
http://www.google.com/intl/en_ALL/mapfiles/closedhand.cur
http://www.google.com/intl/en_ALL/mapfiles/openhand.cur

the format in css should be:

cursor: url(/closedhand.cur) 4 4, move;

Where the “4 4″ are the co-ordinates, and the “move” is the fall-back cursor if the url fails.

In IE it will fail if you have the x/y co-ordinates, so it needs to be like this:

cursor: url(/closedhand.cur), move;

In Opera it will fail completely if you have any of the above :)
So we just need to ovveride for opera.

cursor: move;

either way the code below should be self explanatory:

var dragCursor;
var curBrowser = BrowserDetect.browser;
// IE doesn't support co-ordinates
var cursCoords = (curBrowser == "Explorer") ? "" : " 4 4"; 

if (dragging) {
	dragCursor = (curBrowser == "Firefox") ? "-moz-grabbing" : "url(/closedhand.cur)" + cursCoords + ", move";
	// Opera doesn't support url cursors and doesn't fall back well...
	if (curBrowser == "Opera") dragCursor = "move";
} else {
	dragCursor = (curBrowser == "Firefox") ? "-moz-grab" : "url(/openhand.cur)" + cursCoords + ", move";
}

document.getElementById('objectToDrag').style.cursor = dragCursor;

2 Comments

  1. PWC May 9, 2011 at 9:31 pm #

    Thanks!

  2. Marc July 12, 2011 at 3:38 am #

    I was hoping I could find a css only fix, apparently there’s not one.

    I was having the most trouble with IE9 where the cursor will just disappear when you start to drag if multiple values for cursor are set.

    Looks like I’m gonna have to use your method.

    Cheers…

Post Comment