JSFiddle - React, Tailwind, and code Playground
HTML
<div class="thing">
Click & drag this, releasing the mouse <b>below</b> this box. Then try to make it stop by clicking below.
</div>
<a href="#" id="stop">STOP</a>
CSS
.thing {
padding:10px;
width:200px;
background:tomato;
color:#fff;
}
body {
font-family:sans-serif;
}
JavaScript
/* THIS ISNT WORKING */
$('#stop').on('click',function(){
$('.draggable').off('mousemove');
$('body').append('Im trying!');
});
(function($) {
$.fn.draggable = function(opt) {
opt = $.extend({
cursor: "move"
}, opt);
var $el = this;
return $el.css('cursor', opt.cursor).on("mousedown", function(e) {
var $drag = $(this).addClass('active-handle').addClass('draggable');
var z_idx = $drag.css('z-index'),
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;
$('.draggable').parents().on("mousemove", function(e) {
$('.draggable').offset({
left: e.pageX + pos_x - drg_w
}).on("mouseup", function() {
$(this).removeClass('draggable');;
});
e.stopPropagation();
});
e.preventDefault(); // disable selection
});
}
})(jQuery);
$('.thing').draggable();
$('#stop').one('click', function() {
$('.thing').removeClass('draggable');
})