Last active
September 24, 2015 17:37
-
-
Save tipiirai/783992 to your computer and use it in GitHub Desktop.
The most minimal draggable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var draggable | |
// disable IE specialities | |
document.ondragstart = function () { return false } | |
// bound dragging into document | |
var doc = $(document).bind("mousedown mouseup", function(e) { | |
var el = $(e.target) | |
if (e.type == "mousedown" && el.data("drag")) { | |
var y = el.offset().top - e.pageY | |
doc.bind("mousemove", function(evt) { | |
if (!draggable) { | |
draggable = el.css({position: 'absolute'}) | |
el.trigger("dragstart") | |
} | |
el.css({ top: evt.pageY + y }).trigger("drag") | |
}) | |
e.preventDefault() | |
// mouseup | |
} else if (draggable) { | |
draggable.css({ position: 'static'}).trigger("dragend") | |
doc.unbind("mousemove") | |
draggable = 0 | |
} | |
}) | |
$.fn.mootdrag = function() { | |
return this.data("drag", true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment