Skip to content

Instantly share code, notes, and snippets.

@asolove
Created September 27, 2012 23:46
Show Gist options
  • Save asolove/3797131 to your computer and use it in GitHub Desktop.
Save asolove/3797131 to your computer and use it in GitHub Desktop.
Naive drag and drop
var draggable = $("#drag");
var handle = draggable.find(".handle");
handle.on("mousedown.drag", function mousedown(e){
handle.off("mousedown.drag");
draggable.animate({ opacity: 0.5 });
draggable.on("mouseup.drag", function mouseup(e){
draggable.off("mouseup.drag");
$("body").off("mousemove.drag");
handle.on("mousedown", mousedown);
draggable.animate({ opacity: 1 });
// detect location and possibly do something
});
$("body").on("mousemove.drag", _.throttle(function(e){
draggable.css({ left: e.pageX, top: e.pageY });
}, 100));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment