DragModel——可拖动模态框

实现功能:

  1. 模态框可拖动
  2. 在模态框中打开模态框时,下面的模态框不可点击

在线地址:
https://dddxxxlll.github.io/DragModel/

使用方法:

  1. open_s(id_to, id_from):第一个参数为要打开的目标模态框id,第二个参数为从哪个模态框打开
  2. close_s(id_this, id_to):第一个参数为要关闭的模态框(当前模态框)id,第二个参数为关闭当前模态框后要前往的模态框
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
    #app_content span{display:inline-block;width:130px;text-align:right;}
    #app_content div a{display:inline-block;margin-left:10px;}
    #app_content>div{height:55px;line-height:55px;}
    .cover{width:10000px;height:10000px;background:rgba(200,200,200,0.3);position:absolute;z-index:1050;top:0px;left:0px;}
    /*modal*/
    .closeBtn3{
    cursor:pointer;
    width:30px;
    height:30px;
    border-radius:15px;
    background:#fff;
    color:#00a0e9;
    border:1px solid #00a0e9;
    position:absolute;
    top:-10px;
    line-height:30px;
    right:-10px
    }
    .modalHeader{
    background:rgb(40,134,250);
    color:#fff;
    position:relative;
    text-align:center;
    border-radius:8px 8px 0 0;
    height:45px;
    line-height:45px;
    font-size:18px;
    }
    .modalFooter{
    margin:0 auto
    }
    .modalFooter > div{
    width:150px;
    text-align:center;
    border-radius:4px;
    cursor:pointer;
    margin-top:7px;
    height:35px;
    line-height:35px;
    border:1px solid #399BFF;
    }
    </style>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
    </head>
    <body>
    <button onclick="add_app()">打开模态框</button>
    <!-- 弹出框 -->
    <div id="application" class="modal" style="display:none;position:fixed;top:0;left:0;height:100%;width:100%;background:rgba(0,0,0,0.3)">
    <div class="cover"></div>
    <div id="app_detail" style="width:700px;height:400px;position:absolute;left:50%;top:50%;transform: translate3d(-50%, -50%, 0px);background:#fff;border-radius: 8px;z-index:1060;">
    <div class="modalHeader" style="width:100%;text-align:center;">
    <em class="" style="font-size:20px">添加应用</em>
    <div class="closeBtn3" onclick="close_sp('#application','#app_detail')" style="text-align:center">X</div>
    </div>
    <div class="modalContent" id="app_content" style="height: 280px;">
    <div><span>应用名:</span><input type="text"></div>
    <div><span>应用描述:</span><textarea></textarea></div>
    <div><span>选择服务:</span><a style="cursor:pointer;color: #428bca;text-decoration: none;" onclick="open_s('#servers_detail','#app_detail')" id="recipient_show">请选择</a></div>
    </div>
    <div class="modalFooter" style="border-top:1px solid #ccc; overflow: hidden;margin-top:21px;">
    <div onclick="" style="float:right;margin-right:70px;background:#399BFF;color:#fff;">确认</div> <div onclick="close_sp('#application','#app_detail')" style="float:left;margin-left:70px;">取消</div>
    </div>
    </div>
    <div id="servers_detail" style="display:none;width:700px;height:400px;position:absolute;left:50%;top:50%;transform: translate3d(-50%, -50%, 0px);background:#fff;border-radius: 8px;z-index:1060;">
    <div class="modalHeader" style="width:100%;text-align:center;">
    <em class="" style="font-size:20px">添加应用</em>
    <div class="closeBtn3" onclick="close_s('#servers_detail','#app_detail')" style="text-align:center">X</div>
    </div>
    <div class="modalContent" style="height: 304px;padding: 0 20px;width: 660px;">
    <div class="table app">
    </div>
    </div>
    <div class="modalFooter" style="border-top:1px solid #ccc; overflow: hidden;">
    <div onclick="close_s('#servers_detail','#app_detail')" style="float:right;margin-right:70px;background:#399BFF;color:#fff;">确认</div> <div onclick="close_s('#servers_detail','#app_detail')" style="float:left;margin-left:70px;">取消</div>
    </div>
    </div>
    </div>
    <script>
    function add_app(){
    $('#application').css('display','block');
    /* 增加拖动 */
    $("#app_detail").addClass('adddrag');
    $("#app_detail .modalHeader").addClass('dragarea');
    drag();
    }
    //模态框方法
    function open_s(id,idfrom){
    $(id).css("display","block");
    $(id).css("z-index","1060");
    $(idfrom).css("z-index","1");
    $(".cover").css("display","block");
    $(idfrom).removeClass('adddrag');
    $(idfrom+" .modalHeader").removeClass('dragarea');
    $(id).addClass('adddrag');
    $(id+" .modalHeader").addClass('dragarea');
    drag();
    }
    function close_s(id,idto){
    $(id).css("display","none");
    $(id).css("z-index","1");
    $(idto).css("z-index","1060");
    $(id).removeClass('adddrag');
    $(id+" .modalHeader").removeClass('dragarea');
    $(idto).addClass('adddrag');
    $(idto+" .modalHeader").addClass('dragarea');
    drag();
    }
    function close_sp(id,id2){
    $(id).css("display","none");
    $(id2).removeClass('adddrag');
    $(id2+" .modalHeader").removeClass('dragarea');
    }
    //拖动函数
    function drag() {
    var oDiv = document.getElementsByClassName('adddrag')[0];
    var oDiv2 = document.getElementsByClassName('dragarea')[0];
    // var b = $('.infoBox')[0];
    oDiv2.onmousedown = function(ev){
    var ev = ev || event;
    var disX = ev.clientX - oDiv.offsetLeft;
    var disY = ev.clientY - oDiv.offsetTop;
    if(oDiv2.setCapture){
    oDiv2.setCapture();
    }
    document.onmousemove = function(ev){
    //这里为什么使用document,是因为快速拖拽的话会鼠标丢失,
    var ev = ev || event;
    oDiv.style.left = ev.clientX - disX+"px";
    oDiv.style.top = ev.clientY - disY+"px";
    }
    document.onmouseup = function(ev){
    document.onmousemove = document.onmouseup = null;
    //为何不用oDiv.onmouseup是因为被挡住之后会无视掉遮挡的元素
    if(oDiv2.releaseCapture){
    oDiv2.releaseCapture();
    }
    }
    return false;
    }
    }
    </script>
    </body>
    </html>