Twitter Bootstrap Modal Form: How to drag and drop?

后端 未结 4 605
面向向阳花
面向向阳花 2020-12-08 00:49

I would like to be able to move around (on the greyed-out background, by dragging and dropping) the modal form that is provided by Bootstrap 2. Can anyone tell me what the b

相关标签:
4条回答
  • 2020-12-08 00:57

    Whatever draggable option you go for, you might want to turn off the *-transition properties for .modal.fade in bootstrap’s CSS file, or at least write some JS that temporarily disables them during dragging. Otherwise, the modal doesn’t drag exactly as you would expect.

    0 讨论(0)
  • 2020-12-08 01:09

    You can use a little script likes this.

    simplified from Draggable without jQuery UI

        (function ($) {
            $.fn.drags = function (opt) {
    
                opt = $.extend({
                    handle: "",
                    cursor: "move"
                }, opt);
    
                var $selected = this;
                var $elements = (opt.handle === "") ? this : this.find(opt.handle);
    
                $elements.css('cursor', opt.cursor).on("mousedown", function (e) {
                    var pos_y = $selected.offset().top - e.pageY,
                      pos_x = $selected.offset().left - e.pageX;
                    $(document).on("mousemove", function (e) {
                        $selected.offset({
                            top: e.pageY + pos_y,
                            left: e.pageX + pos_x
                        });
                    }).on("mouseup", function () {
                        $(this).off("mousemove"); // Unbind events from document                
                    });
                    e.preventDefault(); // disable selection
                });
    
                return this;
    
            };
        })(jQuery);
    

    example : $("#someDlg").modal().drags({handle:".modal-header"});

    0 讨论(0)
  • 2020-12-08 01:14

    The bootstrap doesn't come with any dragging and dropping functionality by default, but you can add a little jQuery UI spice into the mix to get the effect you're looking for. For example, using the draggable interaction from the framework you can target your modal ID to allow it to be dragged around within the modal backdrop.

    Try this:

    JS

    $("#myModal").draggable({
        handle: ".modal-header"
    });
    

    Demo, edit here.

    Update: bootstrap3 demo

    0 讨论(0)
  • 2020-12-08 01:15

    jquery UI is large and can conflict with bootstrap.

    An alternative is DragDrop.js: http://kbjr.github.io/DragDrop/index.html

    DragDrop.bind($('#myModal')[0], {
      anchor: $('#myModal .modal-header')
    });
    

    You still have to deal with transitions, as @user535673 suggests. I just remove the fade class from my dialog.

    0 讨论(0)
提交回复
热议问题