繁体   English   中英

Bootstrap 3 模态:使其在没有 jquery-ui 的情况下可移动/可拖动

[英]Bootstrap 3 modal: make it movable/draggable without jquery-ui

我正在尝试开发一些东西:

  • 在没有 jquery-ui 的情况下在窗口上制作引导模式可拖动/可移动

我使用backbone.js,但我认为这不是那么重要。 在一段代码中,我定义了我的引导模式:

<div id="detailsContainer">
<div class="modal fade" id="someId" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<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">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel"><span class="glyphicon glyphicon-cloud-upload" aria-hidden="true"></span> Some Title</h4>
        </div>
        <div class="modal-body" id="content">

            <form class="form-horizontal" role="form" id="modalFormBody">
                ... some content
            </form>    

        </div>
        <div class="modal-footer">
            <div class="pull-right" id="modalButtonBar">
                <button type="button" class="btn btn-default control-button" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary control-button" id="addButton">Add</button>
            </div>                  
        </div>
    </div>
</div>

现在,当我尝试使用以下代码设置模式可拖动时:

 var $container = $("#detailsContainer");
 $container.on('mousedown', 'div', function() {
            $(this).addClass('draggable').parents().on('mousemove', function(e) {
                $('.draggable').offset({
                    top: e.pageY - $('.draggable').outerHeight() / 2,
                    left: e.pageX - $('.draggable').outerWidth() / 2
                }).on('mouseup', function() {
                    $(this).removeClass('draggable');
                });
            });
            e.preventDefault();
        }).on('mouseup', function() {
            $('.draggable').removeClass('draggable');
        });

行为很奇怪:

  • 当我拿起模态时,模态离开窗口。 我试图修复将某些值更改为顶部和左侧的问题,但没有任何效果。
  • 当我选择模态主体的一些下元素(例如输入文本)时,我可以在模态中移动输入文本。 但是我不要这个! 我只希望整个模态可以拖动/移动。

上面的例子是我在 stackoverflow 上找到的(对不起,我没有找到它的链接)。 我还找到了一些 jquery-ui 的例子。 但我不想使用它。 只有jQuery。

我也试过: Dialog draggableJQuery Draggable Demo 但没有成功。 第一个不会使我的模态可拖动,第二个我不明白如何将它与$container一起使用

有人可以帮助我吗?

更新

        $('body').on('mousedown', '.modal-dialog', function() {
             $(this).addClass('draggable').parents().on('mousemove', function(e) {
                 $('.draggable').offset({
                     top: e.pageY - $('.draggable').outerHeight() / 2,
                     left: e.pageX - $('.draggable').outerWidth() / 2
                 }).on('mouseup', function() {
                     $(this).removeClass('draggable');
                 });
             });
             e.preventDefault();
         }).on('mouseup', function() {
             $('.draggable').removeClass('draggable');
         });

但是当我想拖动时......它会改变我在模态中捡起它的位置。 它在拐角处。 不是在那里我捡到的吗? 我该如何纠正? 将“.modal-dialog”放在mousedown函数上是错误的? 如果这是错误的,我必须把它放在哪个元素上?

还有一点:我在模态主体(示例下拉菜单)中的元素不能拖动。 我怎样才能排除它们?

这个答案为时已晚,但以防万一有人(像我一样)正在寻找解决方案。 最后,我的解决方案是使用此代码:

(function ($) {
    $.fn.drags = function (opt) {

        opt = $.extend({ handle: "", cursor: "move" }, opt);

        var $el = null;
        if (opt.handle === "") {
            $el = this;
        } else {
            $el = this.find(opt.handle);
        }

        return $el.css('cursor', opt.cursor).on("mousedown", function (e) {
            var $drag = null;
            if (opt.handle === "") {
                $drag = $(this).parents('.modal-dialog').addClass('draggable');
            } else {
                $drag = $(this).parents('.modal-dialog').addClass('active-handle').parent().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;
            $drag.css('z-index', 1000).parents().on("mousemove", function (e) {
                $('.draggable').offset({
                    top: e.pageY + pos_y - drg_h,
                    left: e.pageX + pos_x - drg_w
                }).on("mouseup", function () {
                    $(this).removeClass('draggable').css('z-index', z_idx);
                });
            });
            e.preventDefault(); // disable selection
        }).on("mouseup", function () {
            if (opt.handle === "") {
                $(this).removeClass('draggable');
            } else {
                $(this).removeClass('active-handle').parent().removeClass('draggable');
            }
        });

    }
})(jQuery);

然后,当显示模态时:

            $('#modal').on('shown.bs.modal', function () {
                $(this).find('.card-header').drags();
            });

            $('#modal').modal({ show: true, backdrop: 'static', keyboard: false });

我在模态对话框中使用了一张带有 HTML 的卡片,如下所示:

<div id="modal" class="modal fade">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content  animated bounceInRight">
            <div class="modal-body">
                <div class="card">
                    <div class="card-header border-bottom-0">My super title</div>
                    <div class="card-body">
                        <form >...</form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-success" id="btnAlmacenar"><i class="fa fa-save"></i> Almacenar</button>
                <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-close"></i> Cancelar</button>
            </div>
        </div>
    </div>
</div>

如果您想尝试,这是整个片段:

 (function ($) { $.fn.drags = function (opt) { opt = $.extend({ handle: "", cursor: "move" }, opt); var $el = null; if (opt.handle === "") { $el = this; } else { $el = this.find(opt.handle); } return $el.css('cursor', opt.cursor).on("mousedown", function (e) { var $drag = null; if (opt.handle === "") { $drag = $(this).parents('.modal-dialog').addClass('draggable'); } else { $drag = $(this).parents('.modal-dialog').addClass('active-handle').parent().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; $drag.css('z-index', 1000).parents().on("mousemove", function (e) { $('.draggable').offset({ top: e.pageY + pos_y - drg_h, left: e.pageX + pos_x - drg_w }).on("mouseup", function () { $(this).removeClass('draggable').css('z-index', z_idx); }); }); e.preventDefault(); // disable selection }).on("mouseup", function () { if (opt.handle === "") { $(this).removeClass('draggable'); } else { $(this).removeClass('active-handle').parent().removeClass('draggable'); } }); } })(jQuery); $(document).ready(function () { $('#modal').on('shown.bs.modal', function () { $(this).find('.card-header').drags(); }); $('#modal').modal({ show: true, backdrop: 'static', keyboard: false }); });
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"></script> <div id="modal" class="modal fade"> <div class="modal-dialog modal-dialog-centered modal-lg"> <div class="modal-content animated bounceInRight"> <div class="modal-body"> <div class="card"> <div class="card-header border-bottom-0">My super title</div> <div class="card-body"> <form></form> </div> </div> </div> <div class="modal-footer"> <button type="submit" class="btn btn-success" id="btnAlmacenar"><i class="fa fa-save"></i> Almacenar</button> <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-close"></i> Cancelar</button> </div> </div> </div> </div>

试试这个代码肯定会帮助你,下面的代码没有 jquery ui

在这里检查这个更新的小提琴检查

 $(function() { $('body').on('mousedown', '#myModal', function(ev) { $(this).addClass('draggable').parents().on('mousemove', function(e) { $('.draggable').offset({ top: e.pageY - $('.draggable').outerHeight() /8, left: e.pageX - $('.draggable').outerWidth() /8 }).on('mouseup', function() { $(this).removeClass('draggable'); }); }); ev.preventDefault(); }).on('mouseup', function() { $('.draggable').removeClass('draggable'); }); });
 body {padding:50px;} div { cursor:move; }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <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">&times;</span></button> <h4 class="modal-title" id="myModalLabel">Modal title</h4> </div> <div class="modal-body"> <form> <div class="form-group"> <label for="recipient-name" class="control-label">Recipient:</label> <input type="text" class="form-control" id="recipient-name"> </div> <div class="form-group"> <label for="message-text" class="control-label">Message:</label> <textarea class="form-control" id="message-text"></textarea> </div> </form> </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> </div> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM