Skip to content

Instantly share code, notes, and snippets.

@mizzao
Forked from keriati/jquery.draggable.js
Last active August 29, 2015 13:58
Show Gist options
  • Save mizzao/9982880 to your computer and use it in GitHub Desktop.
Save mizzao/9982880 to your computer and use it in GitHub Desktop.
That code was a pile of shit, and couldn't handle a lot of things - among others, the handle not being a direct child of the draggable, and binding a billion event handlers, etc
(function($) {
$.fn.drags = function(opt) {
opt = $.extend({handle:"",cursor:"move"}, opt);
if(opt.handle === "") {
var $el = this;
} else {
var $parent = this;
var $el = this.find(opt.handle);
}
return $el.css('cursor', opt.cursor).on("mousedown", function(e) {
if(opt.handle === "") {
var $drag = $(this).addClass('draggable');
} else {
$(this).addClass('active-handle')
var $drag = $parent.addClass('draggable');
}
var
drg_h = $drag.outerHeight(),
drg_w = $drag.outerWidth(),
pos_y = $drag.offset().top + drg_h - e.pageY,
pos_x = $drag.offset().left + drg_w - e.pageX;
follow = function(e) {
$drag.offset({
top:e.pageY + pos_y - drg_h,
left:e.pageX + pos_x - drg_w
})
};
$(window).on("mousemove", follow).on("mouseup", function() {
$drag.removeClass('draggable');
$(window).off("mousemove", follow);
});
e.preventDefault(); // disable selection
}).on("mouseup", function() {
if(opt.handle === "") {
$(this).removeClass('draggable');
} else {
$(this).removeClass('active-handle');
$parent.removeClass('draggable');
}
});
}
})(jQuery);
@mizzao
Copy link
Author

mizzao commented Apr 4, 2014

That code was a pile of shit, and couldn't handle a lot of things - among others, the handle not being a direct child of the draggable, and binding a billion event handlers, etc

Copy link

ghost commented Mar 7, 2015

Thanks for updating this. It seems to work a lot smoother. I've been trying to get this working on a scaled container but it ends up being a bit glitchy. Any ideas on what I could do to get this to work?

Here is a jsfiddle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment