jquery插件封装:弹出div对话框

以前封装了一个Div弹出的jquery插件, jQuery特效之浮动div模式框,这个插件封装的原则追求最简洁,只负责弹出的行为,不负责样式。
而这次的封装是基于上个插件的封装进行进一步的封装,封装一个完整的浮动div弹出插件.先看效果图:
jquery插件封装:弹出div对话框_第1张图片jquery插件封装:弹出div对话框_第2张图片
优势:
- 具有较好的兼容性,兼容IE、chrome、火狐等主流浏览器。
- 智能性较强,例如当设置高度后,body中内容超出范围时候将出现滚动条;当设置弹出框高度大于可视范围时,能够智能调整为自适应浏览器高度等;同时支持点击标题栏可拖拽。
- 调用便捷,只需调用openHaiwiModal和closeHaiwiModal函数即可实现对话框的调用
- 可编程性良好,直接改变定义的div行为进行弹出,而非复制div的html代码,具有较好的可编程性。

使用步骤:
(1)首先引入jquery类库
(2)必须包括样式haiwiModal.css和插件jquery.haiwiModal.js
(3)设置Div模式框的初始显示状态为隐藏


(4)使用$.openHaiwiModal$.closeHaiwiModal弹出关闭Div模式框
  如: $.openHaiwiModal("modalDiv");
    $.closeHaiwiModal("modalDiv");
  另 可以设置options自定义参数,如设置点击遮罩自动层关闭弹出层参数
    $.openHaiwiModal("modalDiv",{closeClickOverlay:true,width:800,height:600});

举例如下:
Html定义模板

<div id="popContainer" class="haiwi-modal">
    <div class="haiwi-modal-title">
       #* 弹出框标题*#
    div>
    <div class="haiwi-modal-body">
        #*弹出框内容*#
    div>
    <div class="haiwi-modal-bottom">
        #*底部按钮*#
        <button class="btn-h28 btn-blue mr20" onclick="save();"><strong>保存strong>button>
        <button class="btn-h28 btn-white" onclick="closeAddDialog();">取 消button>
    div>
    <a href="javascript:void(0);" class="close-haiwi-modal">×a>
div>

javascript调用过程

//打开
$.openHaiwiModal("popContainer");
或
$.openHaiwiModal("popContainer",{closeClickOverlay:true,width:800,height:600});

//关闭
$.closeHaiwiModal("popContainer");

源码:
CSS样式表

.haiwi-modal-overlay{
    position:fixed;
    z-index:10000;
    top:0px;
    left:0px;
    height:100%;
    width:100%;
    background:#000;
    display:none;
}

.haiwi-modal {
    position: relative;
    display: none;
    background-color: #FFFFFF;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-box-shadow: 0 0 10px rgba(0,0,0,.4);
    -webkit-box-shadow: 0 0 10px rgba(0,0,0,.4);
    -box-shadow: 0 0 10px rgba(0,0,0,.4);
    border: 1px solid #d3d3d3;
}

.haiwi-modal .haiwi-modal-title{
    position: absolute;
    display: block;
    font-size: 2em;
    -webkit-margin-start: 0px;
    -webkit-margin-end: 0px;
    font-weight: bold;
    border-bottom: 1px solid #ddd;
    top: 0px;
    left: 30px;
    right: 30px;
    padding-top: 25px;
    padding-bottom: 10px;
    cursor:pointer;
}

.haiwi-modal .haiwi-modal-body{
    position: absolute;
    top: 60px;
    left: 0px;
    right: 1px;
    bottom:55px;
    color: #404040;
    padding-top: 20px;
    padding-left: 30px;
    padding-right: 30px;
    text-align: left;
}

.haiwi-modal .close-haiwi-modal {
    position: absolute;
    font-size: 22px;
    line-height: .5;
    top: 12px;
    right: 13px;
    color: #aaa;
    text-shadow: 0 -1px 1px rbga(0,0,0,.6);
    font-weight: bold;
    cursor: pointer;
    text-decoration: none;
    filter: alpha(opacity=60);
    -khtml-opacity: 0.6;
    -moz-opacity: 0.6;
    opacity: 0.6;
}


.haiwi-modal .close-haiwi-modal:hover, .haiwi-modal .close-haiwi-modal:focus{
    -khtml-opacity: 1.0;
    -moz-opacity: 1.0;
    opacity: 1.0;
}

.haiwi-modal .haiwi-modal-bottom {
    position: absolute;
    text-align: center;
    -webkit-border-radius: 0 0 6px 6px;
    -moz-border-radius: 0 0 6px 6px;
    border-radius: 0 0 6px 6px;
    -webkit-box-shadow: inset 0 1px 0 #ffffff;
    -moz-box-shadow: inset 0 1px 0 #ffffff;
    box-shadow: inset 0 1px 0 #ffffff;
    border-top: 1px solid #ddd;
    padding: 10px 0 0;
    zoom: 1;
    left: 30px;
    right: 30px;
    bottom: 15px;
}

.btn-h28,.btn-h22,.btn-h38 {
    display: inline-block;
    border: 1px solid;
    *zoom: 1;
    font-size: 12px;
    border-radius: 5px;
    vertical-align: middle;
    cursor: pointer;
    transition: background .3s;
    padding-left: 12px;
    padding-right: 12px;
    outline: 0
}

input.btn-h28,input.btn-h22,input.btn-h38,button.btn-h28,button.btn-h22,button.btn-h38 {
    box-sizing: content-box;
    *overflow: visible
}

a.btn-h28:hover,a.btn-h22:hover,a.btn-h38:hover {
    text-decoration: none
}

.btn-h28 {
    height: 28px;
    line-height: 28px
}

html* input.btn-h28,html* button.btn-h28 {
    height: 30px
}

.btn-h22 {
    height: 22px;
    line-height: 22px
}

html* input.btn-h22,html* button.btn-h22 {
    height: 24px
}

.btn-h38 {
    font-size: 14px;
    height: 38px;
    line-height: 38px
}

html* input.btn-h38,html* button.btn-h38 {
    height: 40px
}

.btn-blue {
    background-color: #48a7e7;
    border-color: #48a7e7;
    color: #fff
}

.btn-blue:hover {
    background-color: #4196cf
}

a.btn-blue:link,a.btn-blue:visited,a.btn-blue:hover {
    color: #fff
}

a.btn-blue:hover {
    background-color: #4196cf
}

.btn-blue:hover {
    background-color: #4196cf
}

.btn-green {
    background-color: #2bc762;
    border-color: #2bc762;
    color: #fff
}

.btn-green:hover {
    background-color: #26ac56
}

a.btn-green:link,a.btn-green:visited,a.btn-green:hover {
    color: #fff
}

.btn-white {
    background-color: #fff;
    border-color: #c7c7c7;
    color: #333
}

.btn-white:hover {
    background-color: #eee
}

a.btn-white:link,a.btn-white:visited,a.btn-white:hover {
    color: #333
}

.btn-blue-n {
    background-color: #badef5;
    border-color: #badef5;
    color: #d4eaf9;
    cursor: default
}

.btn-disabled {
    border-color: #c6c6c6;
    color: #c6c6c6;
    cursor: default;
    background-color: #fff
}


.ml10 {
    margin-left: 10px
}

.ml20 {
    margin-left: 20px
}

.mr10 {
    margin-right: 10px
}

.mr20 {
    margin-right: 20px
}

Jquery源码

/**Haiwi@20160616
 *弹出DIV遮罩层(只能弹出页面中的元素)的模式对话框,由开源JS库jquery.leanModal.min.js改造而来
 *使用方式:
 (1)必须包括样式haiwiModal.css
 (2)设置Div模式框的初始显示状态为隐藏
 
(3)使用$.openHaiwiModal和$.closeHaiwiModal弹出关闭Div模式框 如: $.openHaiwiModal("modalDiv"); $.closeHaiwiModal("modalDiv"); 另 可以设置options自定义参数,如设置点击遮罩自动层关闭弹出层参数 $.openHaiwiModal("modalDiv",{closeClickOverlay:true,width:800,height:600}); */
$.extend({ openHaiwiModal:function(div_id,options){ var defaults={ top:100, //距离顶部的高度 overlay:0.5, //遮罩度 closeClickOverlay:false, //点击遮罩层是否自动关闭 isDrag:true, //是否可拖动 width:400, //弹出框宽度 height:300 //弹出框高度 }; $("#haiwi-modal-overlay_"+div_id).remove(); var zIndex =10000+$(".haiwi-modal-overlay").length*2; var overlay=$('
'" style="z-index:'+zIndex+'">
'
); $("body").append(overlay); defaults=$.extend(defaults,options); return $("#"+div_id).each(function(){ var o=defaults; var modal_id="#" + div_id; var modal_title ="#" + div_id + " .haiwi-modal-title"; var modal_body = "#" + div_id + " .haiwi-modal-body"; var modal_bottom = "#" + div_id + " .haiwi-modal-bottom"; var modal_close = "#" + div_id+" .close-haiwi-modal"; if($(modal_id).outerWidth()< o.width){ $(modal_id).css({"width":o.width}); } if($(modal_id).outerHeight()"height":o.height}); } var modal_width=$(modal_id).outerWidth(); var modal_height=$(modal_id).outerHeight(); $("#haiwi-modal-overlay_"+div_id).css({"display":"block",opacity:0}); $("#haiwi-modal-overlay_"+div_id).fadeTo(200,o.overlay); $(modal_body).css({"overflow-y":"auto"}); $(modal_id).css({"display":"block","position":"fixed","opacity":0,"z-index":zIndex+1,"left":50+"%","margin-left":-(modal_width/2)+"px","top":o.top+"px"}); $(modal_id).fadeTo(200,1); if(o.isDrag){ //点击标题头进行拖动对话框 var _move=false;//移动标记 var _x= 0,_y=0; $(modal_title).click(function(){ }).mousedown(function(e){ _move=true; _x=e.pageX-parseInt($(modal_id).css("left")); _y=e.pageY-parseInt($(modal_id).css("top")); }); $(document).mousemove(function(e){ if(_move){ var x=e.pageX-_x;//移动时根据鼠标位置计算控件左上角的绝对位置 x=x<0 ? 0 : x; var y=e.pageY-_y; y=y<0 ? 0 : y; y=y>$(window).height()-20 ? $(window).height()-20 : y; $(modal_id).css({top:y,left:x});//控件新位置 } }).mouseup(function(){ _move=false; }); } if($(modal_id).height() + o.top>$(window).height()){ $(modal_id).css({"top":0}); } if($(modal_id).height()>$(window).height()){ $(modal_id).css({"height":$(window).height()-20,"top":10}); } if($(modal_title).length==0){ $(modal_body).css({"top":0}); } if($(modal_bottom).length==0){ $(modal_body).css({"bottom":0}); } if(o.closeClickOverlay){ $("#haiwi_overlay").click(function(){ $.closeHaiwiModal(div_id); }); } $(modal_close).click(function(){ $.closeHaiwiModal(div_id); }); }); }, closeHaiwiModal:function(div_id){ $("#haiwi-modal-overlay_"+div_id).remove(); $("#"+div_id).css({"display":"none"}); } });

点击查看源码:源码

你可能感兴趣的:(前端)