bootstrap modal可以拖动
时间: 2023-08-03 20:09:37 浏览: 242
BootStrap modal实现拖拽功能
是的,Bootstrap Modal可以通过添加一些JavaScript代码使其可拖动。你需要为Modal添加一个mousedown事件监听器,当鼠标按下时,记录鼠标的位置。然后在mousemove事件中,计算鼠标的偏移量,并使用CSS将Modal移动到新位置。最后,在mouseup事件中,停止移动Modal并取消事件监听器。以下是一个示例代码:
```javascript
$('#myModal').on('show.bs.modal', function () {
var modal = $(this);
var draggable = modal.find('.modal-dialog');
draggable.css('top', '');
draggable.css('left', '');
modal.on('mousedown', function (event) {
var isDraggable = $(event.target).closest('.modal-header, .modal-dialog').length > 0;
if (!isDraggable) {
return;
}
var offsetX = event.clientX - parseFloat(draggable.css('left'));
var offsetY = event.clientY - parseFloat(draggable.css('top'));
$(document).on('mousemove.draggable', function (event) {
draggable.css('left', event.clientX - offsetX);
draggable.css('top', event.clientY - offsetY);
});
$(document).on('mouseup.draggable', function () {
$(document).off('mousemove.draggable mouseup.draggable');
});
});
});
```
这段代码可以让Modal的标题栏和内容区域都可拖动。
阅读全文