动态创建弹出层并且弹出层额内容样式可自定义

来源:互联网 发布:上汽集团数据业务部 编辑:程序博客网 时间:2024/06/06 20:44

不知大家在开发中是否遇到这样的需求,在同一项目中需要弹出风格大致一致但是内容及样式有极大更改的大弹出层。引用插件太麻烦且样式和内容不是那么可控。今天我实现了一个动态创建弹出层的效果大概是这样的,在点击需要弹出层的按钮后动态创建一个弹出层,其样式和内容均可写在另外一个HTML页面通过AJAX请求得到,弹出层在页面居中。

先贴上jq源码,第一个参数代表请求的地址(也就是你写的html的地址),第二个参数是附加数据,第三个是数据类型在这里写html就好了,第三个是你设置的弹出层的宽高,也可以设置一个表示宽高均是此值,第四个参数代表回调函数:

function definedLayer(url,data,dataType,styleXY,callback){
if(!$(".alert-ui-yaler").size()){
$(document.body).append('<div class="alert-ui-yaler"></div>');
};
if(!$(".alert-ui-dialog").size()){
$(document.body).append('<div class="alert-ui-dialog"><span class="layer-dialog-close-btn" title="关闭"><i>*</i></span></div>');
}
var setWidth=null;
var setHeight=null;
/*根据设置的宽高值参数来设置值*/
if(styleXY&&typeof(styleXY)==='object'){
console.log(styleXY.length);
if(styleXY.length==2){
setWidth=styleXY[0];
setHeight=styleXY[1]
console.log(setWidth,setHeight);
}else{
setWidth=styleXY[0];
setHeight=styleXY[0]
}
}
//支持设置宽高的百分比
if(typeof(setWidth)=='string'&&setWidth.indexOf("%")>0){
setWidth=$(window).width()/100*(parseFloat(setWidth.replace("%","")));
}
if(typeof(setHeight)=='string'&&setHeight.indexOf("%")>0){
setHeight=$(window).width()/100*(parseFloat(setHeight.replace("%","")));
}
$(".alert-ui-dialog").attr("data-width",setWidth).attr("data-height",setHeight);
if(typeof(setWidth)!="number"&&typeof(setHeight)!="number"){
alert("输入的数字不是数字类型!");
$(".alert-ui-dialog").css({
"max-height":($(window).height()-40)+"px",
"height":($(window).height()-40)+"px",
"width":($(window).width()-40)+"px",
"left":"50%",
"margin-left":-(($(window).width()-40)>1000?$(".alert-ui-dialog").outerWidth():($(window).width()-40)/2)
})
}else{
alert("进入到这里判断!");
$(".alert-ui-dialog").css({
"max-height":($(window).height()-40)+"px",
"max-width":($(window).width()-40)+"px",
"height":setHeight=="auto"?($(window).height()-40)+"px":setHeight+"px",
"width":setWidth=="auto"?($(window).width()-40)+"px":setWidth+"px",
"left":"50%",
"margin-left":-(($(window).width()-40)>1000?setWidth/2:($(window).width()-40)/2)
})
   console.log($(".alert-ui-dialog").width(),$(".alert-ui-dialog").height());
}
data=data||{};
$.ajax({
type:"GET",
url:url,
async:false,
data:data,
dataType:dataType,
beforeSend:function(){
console.log("fasong");
},
success:function(data){
setTimeout(function(){
$(".alert-ui-dialog").append(data)
},50);
},
error:function(){
console.log("chucuo");
}
});
$(document).on("click",".layer-dialog-close-btn",function(){
$(".alert-ui-dialog").remove();
$(".alert-ui-yaler").remove();
   //关闭时弹出回调函数
  if(callback&&typeof(callback)==="function"){
    callback();
  }
})
}

接下来贴上css样式:

*{margin: 0;padding: 0;}
.alert-ui-yaler{width:100%;height: 100%;position: fixed;left: 0;top: 0;right: 0;bottom: 0;z-index: 86;background: rgba(0,0,0,0.4);}
.alert-ui-dialog{position: fixed;top: 20px;background: #008B8B;z-index: 88;border: 5px solid pink;overflow-y: scroll;overflow-x: hidden;}
.layer-dialog-close-btn{position:absolute;top:0px;right: 0px;z-index: 99;font-size: 20px;width: 24px;height: 24px;cursor: pointer;text-align: center;background: red;display: inline-block;line-height: 24px;border-radius: 5px;


@media only screen and (max-width:1200px ) {
bottom:20px;

}

接着是调用弹出层的html页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="layer.css" />
<script type="text/javascript" src="jquery.min.js" ></script>
<script type="text/javascript" src="layer.js" ></script>

</head>
<body>
<button onclick="definedLayer('layer-test.html',{id:0},'html',[600,800])" >弹出层</button>
</body>
</html>

最后是你自定义的弹出内容是一个html代码片段:

<div class="con" style="width:800px;height: 1000px;background: yellowgreen;">1111</div>

需要给大家提醒的是基本功能已经有了,但是一些细节还需要完善,比如说调节窗口时对应弹出层位置变化等,还有更多处理bugger的地方尚未解决!我的博客里有对应源码下载!


0 0
原创粉丝点击