make bootstrap twitter dialog modal draggable

后端 未结 9 2215
野性不改
野性不改 2021-01-30 08:43

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.

<
9条回答
  •  悲哀的现实
    2021-01-30 08:53

    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;
    }
    
    
    
    
    

提交回复
热议问题