实用的div弹出层

使用div实现的弹出层效果,带遮罩层和透明度效果

代码:

(function(){

if(window.popwindow)return;

var rootEl = null;

var maskel = null;

var winel = null;

window.popwindow = {

_init:function(){

rootEl = document.body;

rootEl.appendChild(div);

maskel = $('fill-mask');

winel = $('fill-window');

maskel.attachEvent("onclick",function(){

setStyle(maskel,{

display:'none'

});

setStyle(winel,{

display:'none'

});

});

},

show:function(cfg){

this._init();

var pos = getPos(cfg.width,cfg.height);

if(cfg.ajax){

toServer({

url:cfg.url,

param:cfg.param

});

}else{

winel.innerHTML = cfg.src;

}

setStyle(winel,{

left:(pos[0])+'px',

top:(pos[1])+'px',

width:cfg.width+'px',

height:cfg.height+'px',

display:''

});

var winSize = getWinSize();

setStyle(maskel,{

width:winSize[0]+'px',

height:winSize[1]+'px',

display:''

});

}

}

var $ = function(id){return document.getElementById(id)};

var div = document.createElement("<div>");

var strHTML = "";

strHTML += "<div id='fill-mask' oncontextmenu='return false;' onselectstart='return false;' style='position:absolute;z-index:10000;top:0;left:0;filter:alpha(opacity=50);background:#000'></div>";

strHTML += "<div id='fill-window' oncontextmenu='return false;' onselectstart='return false;' style='position:absolute;z-index:10001;'></div>";

div.innerHTML = strHTML;

var getPos = function(width,height){

var winSize = getWinSize();

var offWidth = winSize[0];

var offHeight = winSize[1];

var clientX = window.event.clientX+10;

var clientY = window.event.clientY+10;

if(clientX+width > offWidth){

clientX = clientX - width - 20;

}

if(clientY+height > offHeight){

clientY = clientY - height - 20;

}

return [clientX,clientY];

}

var getWinSize = function() {

return [Math.max(document.documentElement.scrollWidth,document.documentElement.clientWidth), Math.max(document.documentElement.scrollHeight,document.documentElement.clientHeight)]

};

var setStyle = function(el,o){

for (var i in o)el.style[i] = o[i]

}

var toServer = function(obj){

var xhr = false;

if(window.ActiveXObject){

xhr = new ActiveXObject("Microsoft.XMLHTTP");

}else if(window.XMLHttpRequest){

xhr = new XMLHttpRequest();

}

xhr.open("POST",obj.url);

xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

xhr.send(obj.param);

xhr.onreadystatechange=function(){

if(xhr.readyState==4){

if(xhr.status==200){

var rv= xhr.responseText; 

   if(rv == "")return false;

   winel.innerHTML = rv;

}

}

}

}

})();

 

测试页面:

<html>

<head>

<script type="text/javascript" src="popwindow.js"></script>

<script type="text/javascript">

function showMenu(){

popwindow.show({

width:200,

height:150,

ajax:false,

url:"",//可以ajax获取jsp资源

param:"",

src:"<div style='width:200px;height:100px;background-color:orange;'>哇哈哈</div>"

});

}

</script>

</head>

<body>

  <input type="button" name="test" value="test" onclick="showMenu();">

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值