I\'m trying to make bootstrap twitter dialog modal draggable with this jquery plugin:
http://threedubmedia.com/code/event/drag#demos
but it doesn\'t work.
<You can use the code below if you dont want to use jQuery UI or any third party pluggin. It's only plain jQuery.
This answer works well with Bootstrap v3.x . For version 4.x see @User comment below
$(".modal").modal("show");
$(".modal-header").on("mousedown", function(mousedownEvt) {
var $draggable = $(this);
var x = mousedownEvt.pageX - $draggable.offset().left,
y = mousedownEvt.pageY - $draggable.offset().top;
$("body").on("mousemove.draggable", function(mousemoveEvt) {
$draggable.closest(".modal-dialog").offset({
"left": mousemoveEvt.pageX - x,
"top": mousemoveEvt.pageY - y
});
});
$("body").one("mouseup", function() {
$("body").off("mousemove.draggable");
});
$draggable.closest(".modal").one("bs.modal.hide", function() {
$("body").off("mousemove.draggable");
});
});
.modal-header {
cursor: move;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
$("#myModal").draggable({
handle: ".modal-header"
});
it works for me. I got it from there. if you give me thanks please give 70% to Andres Ilich
i did this:
$("#myModal").modal({}).draggable();
and it make my very standard/basic modal draggable.
not sure how/why it worked, but it did.
For the mouse cursor (with jQuery UI) :
$('#my-modal').draggable({
handle: ".modal-header"
});
For the touch cursor (with jQuery):
var box = null;
var touchobj = null;
var position = {'x':0, 'y':0};
var positionbox = {'x':0, 'y':0};
// init touch
$('.modal-header').on('touchstart', function(e){
box = $(this).closest('.modal-dialog');
touchobj = e.changedTouches[0];
// take position touch cursor
position['x'] = touchobj.pageX;
position['y'] = touchobj.pageY;
//take original position box to move with touch
positionbox['x'] = parseInt(box.css('left'));
positionbox['y'] = parseInt(box.css('top'));
e.preventDefault();
});
// on move touch
$('.modal-header').on('touchmove', function(e){
var dist = {'x':0, 'y':0};
touchobj = e.changedTouches[0];
// we calculate the distance of move
dist['x'] = parseInt(touchobj.clientX) - position['x'];
dist['y'] = parseInt(touchobj.clientY) - position['y'];
// we apply the movement distance on the box
box.css('left', positionbox['x']+dist['x'] +"px");
box.css('top', positionbox['y']+dist['y'] +"px");
e.preventDefault();
});
The addition of the 2 solutions is compatible
In my case I am enabling draggable. It works.
var bootstrapDialog = new BootstrapDialog({
title: 'Message',
draggable: true,
closable: false,
size: BootstrapDialog.SIZE_WIDE,
message: 'Hello World',
buttons: [{
label: 'close',
action: function (dialogRef) {
dialogRef.close();
}
}],
});
bootstrapDialog.open();
Might be it helps you.
The top-ranked solution (by Mizbah Ahsan) is not quite right ...but is close. If you apply draggable() to the modal dialog element, the browser window scroll bars will drag around the screen as you drag the modal dialog. The way to fix that is to apply draggable() to the modal-dialog class instead:
$(".modal-dialog").draggable({
handle: ".modal-header"
});
Thanks!