Skip to content

Instantly share code, notes, and snippets.

@tatat
Created January 5, 2013 10:51
Show Gist options
  • Save tatat/4460963 to your computer and use it in GitHub Desktop.
Save tatat/4460963 to your computer and use it in GitHub Desktop.
!function($, start_x, start_y, target_start_x, target_start_y, $target) {
$(document).on('mousedown', function(e){
var style, target = e.target;
do {
style = document.defaultView.getComputedStyle(target, null);
} while (style.position !== 'absolute' && (target = target.parentNode));
if (!target)return;
$target = $(target);
var position = $target.position();
start_x = e.pageX;
start_y = e.pageY;
target_start_x = position.left;
target_start_y = position.top;
console.log(target);
e.stopPropagation();
e.preventDefault();
}).on('mousemove', function(e) {
if (!$target) return;
$target.css({
left: target_start_x + (e.pageX - start_x) + 'px'
, top: target_start_y + (e.pageY - start_y) + 'px'
});
e.stopPropagation();
e.preventDefault();
}).on('mouseup', function(e) {
if (!$target) return;
var position = $target.position();
console.log(position.top, position.left);
$target = null;
e.stopPropagation();
e.preventDefault();
});
}(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment