(function ($) { $.fn.draggable = function (controlelement) { var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; var elmnt = $(this), ce = controlelement instanceof jQuery ? controlelement : elmnt.find(controlelement); function dragMouseDown(e) { e.stopPropagation(); e.preventDefault(); // get the mouse cursor position at startup: pos3 = e.clientX; pos4 = e.clientY; document.onmouseup = closeDragElement; // call a function whenever the cursor moves: document.onmousemove = elementDrag; } function elementDrag(e) { e = e || window.event; e.preventDefault(); // calculate the new cursor position: pos1 = pos3 - e.clientX; pos2 = pos4 - e.clientY; pos3 = e.clientX; pos4 = e.clientY; // set the element's new position: elmnt.css('top', (elmnt.offset().top - pos2) + 'px'); elmnt.css('left', (elmnt.offset().left - pos1) + 'px'); } function closeDragElement() { // stop moving when mouse button is released: document.onmouseup = null; document.onmousemove = null; } if (typeof controlelement !== 'undefined' && ce.length > 0) { ce.mousedown(dragMouseDown).addClass('dctrl'); } else { elmnt.mousedown(dragMouseDown).addClass('dctrl'); } return $(this); } })(jQuery);