Skip to content

Instantly share code, notes, and snippets.

@tipiirai
Last active September 24, 2015 17:37
Show Gist options
  • Save tipiirai/783992 to your computer and use it in GitHub Desktop.
Save tipiirai/783992 to your computer and use it in GitHub Desktop.
The most minimal draggable
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