// Set up the variables
var mouseDown = false;
var offset = {x:0, y:0};
// IE Selection fix
$('div.draggable').attribute().onselectstart = function () {
return false;
}
// Good browser selection fix
$('div.draggable').attribute().onmousedown = function () {
return false;
}
// On mouse down, set the listener to true
$('div.draggable').event('mousedown', function(e) {
mouseDown = true;
offset.x = e.pageX - $('div.draggable').location().x;
offset.y = e.pageY - $('div.draggable').location().y;
});
// On mouse up, set the listener to false
$('body').event('mouseup', function(e) {
mouseDown = false;
});
// On mouse move, if the listener is true, move the div
$('body').event('mousemove', function(e) {
if(mouseDown == true)
$('div.draggable').location(e.pageX - offset.x, e.pageY - offset.y);
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.