js弹出层/框 收集
1、可关闭,不可拖动
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
- <title>JS弹出层</title>
- <script language="javascript">
- <!--
- function BOX_show(e){ //显示
- if(document.getElementById(e)==null){
- return ;
- }
- var selects = document.getElementsByTagName('select');
- for(i = 0; i < selects.length; i++){
- selects[i].style.visibility = "hidden";
- }
- BOX_layout(e);
- window.onresize = function(){BOX_layout(e);} //改变窗体重新调整位置
- window.onscroll = function(){BOX_layout(e);} //滚动窗体重新调整位置
- document.onkeyup = function(event){
- var evt = window.event || event;
- var code = evt.keyCode?evt.keyCode : evt.which;
- //alert(code);
- if(code == 27){
- BOX_remove(e);
- }
- }
- }
- function BOX_remove(e){ //移除
- window.onscroll = null;
- window.onresize = null;
- document.getElementById('BOX_overlay').style.display="none";
- document.getElementById(e).style.display="none";
- var selects = document.getElementsByTagName('select');
- for(i = 0; i < selects.length; i++){
- selects[i].style.visibility = "visible";
- }
- }
- function BOX_layout(e){ //调整位置
- var a = document.getElementById(e);
- if (document.getElementById('BOX_overlay')==null){ //判断是否新建遮掩层
- var overlay = document.createElement("div");
- overlay.setAttribute('id','BOX_overlay');
- //overlay.onclick=function(){BOX_remove(e);};
- //a.parentNode.appendChild(overlay);
- document.body.appendChild(overlay);
- }
- document.getElementById('BOX_overlay').ondblclick=function(){BOX_remove(e);};
- var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); //取客户端左上坐标,宽,高
- var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
- var clientWidth;
- if (window.innerWidth){
- clientWidth = window.innerWidth;
- // clientWidth = ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
- }
- else{
- clientWidth = document.documentElement.clientWidth;
- }
- var clientHeight;
- if (window.innerHeight){
- clientHeight = window.innerHeight;
- //clientHeight = ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
- }
- else{
- clientHeight = document.documentElement.clientHeight;
- }
- var bo = document.getElementById('BOX_overlay');
- bo.style.left = scrollLeft+'px';
- bo.style.top = scrollTop+'px';
- bo.style.width = clientWidth+'px';
- bo.style.height = clientHeight+'px';
- bo.style.display="";
- a.style.position = 'absolute'; //Popup窗口定位
- a.style.zIndex=999;
- a.style.display="";
- //a.style.left = scrollLeft+((clientWidth-a.offsetWidth)/2)+'px';
- //a.style.top = scrollTop+((clientHeight-a.offsetHeight)/2)+'px';
- }
- function HiddenButton(e){
- e.style.visibility='hidden';
- e.coolcodeviousSibling.style.visibility='visible'
- }
- //-->
- </script>
- <style type="text/css">
- <!--
- #BOX_overlay{position: absolute;z-index:100;top:0px;left:0px;background-color:#ccc;filter:alpha(opacity=70);-moz-opacity:0.7;opacity:0.7;}
- -->
- </style>
- </head>
- <body>
- <p onClick="BOX_show('messdiv')" style="cursor:pointer;">点击弹出层</p>
- <div id="messdiv" style="position:absolute; display:none; top:135px; left:350px; width:560px; padding:10px 20px 0; background:#369;text-align:left; border:2px solid #5d3f0c;z-index:999; font-size:12px;">
- <p style="text-align:right; margin:0; padding:0; line-height:14px; float:right;">
- <a href="javascript:void(0)" title="关闭" style="line-height:14px;font-size:12px; color:#333;" onclick="BOX_remove('messdiv')" target="_self">关闭</a>
- </p>
- <br /><br /><br /><br />
- 这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
- <br /><br /><br /><br />
- <br /><br /><br /><br />
- <div style="width:176px;padding:0 120px;overflow:hidden;margin:10px auto 20px;">
- <a href="javascript:void(0)" onclick="BOX_remove('messdiv')" target="_self" style=" background:#fff;float:left; width:70px;height:20px;padding:5px 0 0 10px;margin-right:10px;">关闭</a>
- <a href="javascript:void(0)" onClick="BOX_remove('messdiv')" target="_self" style=" background:#fff;float:left;width:70px;height:20px;padding:5px 0 0 10px;">取消</a>
- </div>
- <div style="clear:both;"></div>
- </div>
- <div id="BOX_overlay"></div>
- </body>
- </html>
2、可关闭,可拖动,不可拉伸
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>弹出层并可拖拽</title>
- <style>
- html,body{height:100%;overflow:hidden;}
- body,div,h2{margin:0;padding:0;}
- body{font:12px/1.5 Tahoma;}
- center{padding-top:10px;}
- button{cursor:pointer;}
- #overlay{position:absolute;top:0;left:0;width:100%;height:100%;background:#000;opacity:0.5;filter:alpha(opacity=50);display:none;}
- #win{position:absolute;top:50%;left:50%;width:400px;height:200px;background:#fff;border:4px solid #f90;margin:-102px 0 0 -202px;display:none;}
- h2{font-size:12px;height:18px;text-align:right;background:#FC0;border-bottom:3px solid #f90;padding:5px;cursor:move;}
- h2 span{color:#f90;cursor:pointer;background:#fff;border:1px solid #f90;padding:0 2px;}
- </style>
- <script>
- window.onload = function ()
- {
- var oWin = document.getElementById("win");
- var oLay = document.getElementById("overlay");
- var oBtn = document.getElementsByTagName("button")[0];
- var oClose = document.getElementById("close");
- var oH2 = oWin.getElementsByTagName("h2")[0];
- var bDrag = false;
- var disX = disY = 0;
- oBtn.onclick = function ()
- {
- oLay.style.display = "block";
- oWin.style.display = "block"
- };
- oClose.onclick = function ()
- {
- oLay.style.display = "none";
- oWin.style.display = "none"
- };
- oClose.onmousedown = function (event)
- {
- (event || window.event).cancelBubble = true;
- };
- oH2.onmousedown = function (event)
- {
- var eventevent = event || window.event;
- bDrag = true;
- disX = event.clientX - oWin.offsetLeft;
- disY = event.clientY - oWin.offsetTop;
- this.setCapture && this.setCapture();
- return false
- };
- document.onmousemove = function (event)
- {
- if (!bDrag) return;
- var eventevent = event || window.event;
- var iL = event.clientX - disX;
- var iT = event.clientY - disY;
- var maxL = document.documentElement.clientWidth - oWin.offsetWidth;
- var maxT = document.documentElement.clientHeight - oWin.offsetHeight;
- iLiL = iL < 0 ? 0 : iL;
- iLiL = iL > maxL ? maxL : iL;
- iTiT = iT < 0 ? 0 : iT;
- iTiT = iT > maxT ? maxT : iT;
- oWinoWin.style.marginTop = oWin.style.marginLeft = 0;
- oWin.style.left = iL + "px";
- oWin.style.top = iT + "px";
- return false
- };
- document.onmouseup = window.onblur = oH2.onlosecapture = function ()
- {
- bDrag = false;
- oH2.releaseCapture && oH2.releaseCapture();
- };
- };
- </script>
- </head>
- <body>
- <div id="overlay"></div>
- <div id="win"><h2><span id="close">×</span></h2></div>
- <center><button>弹出层</button></center>
- </body>
- </html>
3、可关闭、可拖动、可拉伸
注:当中有三个图片,可打包下载
- <!DOCTYPE html PUBLIC "-//W3C//h2D XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/h2D/xhtml1-transitional.h2d">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
- <title>可拖拽变大小的弹出层</title>
- <style>
- <!--
- * { padding: 0; margin: 0; }
- li { list-style: none; }
- body { background: #eee; }
- .drag_layer { width: 450px; height: 270px; border: 1px solid #aaaaaa; padding: 3px; position: absolute; top: 0px; left: 0px; background: #fff; overflow:hidden; }
- .drag_layer h2 { height: 25px; line-height: 25px; padding-left: 10px; font-size: 14px; color: #333; background: url(images/title_bg.gif) repeat-x; border: 1px solid #aaaaaa; cursor: move; position: relative; }
- .drag_layer .close { width: 21px; height: 20px; background: url(images/close.gif) no-repeat 0 bottom; position: absolute; top: 2px; right: 3px; }
- .drag_layer .close:hover { background: url(images/close.gif) no-repeat 0 0; }
- .drag_layer .content { padding: 10px; overflow:auto; }
- .drag_layer .content p { font-size: 12px; line-height: 18px; color: #666; text-indent: 28px; padding-bottom: 10px; font-family: arial; }
- .drag { width: 14px; height: 14px; overflow: hidden; background: url(images/drag_ico.gif) no-repeat 1px 1px; cursor: nw-resize; position: absolute; top: 262px; right: 0; z-index: 2; }
- .bar_t { width: 410px; height: 4px; position: absolute; top: -2px; left: -2px; z-index: 1; cursor: n-resize; overflow: hidden; }
- .bar_b { width: 410px; height: 4px; position: absolute; bottom: -2px; left: -2px; z-index: 1; cursor: n-resize; overflow: hidden; }
- .bar_r { width: 4px; height: 110px; position: absolute; right: -2px; top: -2px; z-index: 1; cursor: e-resize; }
- .bar_l { width: 4px; height: 110px; position: absolute; left: -2px; top: -2px; z-index: 1; cursor: e-resize; }
- -->
- </style>
- <script>
- <!--
- var oMsgbox=null;
- var g_orgTop=0;
- var g_orgHeight=0;
- var g_orgLeft=0;
- var g_orgWidth=0;
- var oContent=null;
- var oDragBoth=null;
- var oDragL=null;
- var oDragT=null;
- var oDragR=null;
- var oDragB=null;
- window.onload=function (){
- var oBtnClose=null;
- var oH2Title=null;
- var aDiv=null;
- var fnDrag=null;
- var i=0;
- oMsgbox=document.getElementById('msgbox');
- oBtnClose=oMsgbox.getElementsByTagName('a')[0];
- oH2Title=oMsgbox.getElementsByTagName('h2')[0];
- aDiv=oMsgbox.getElementsByTagName('div');
- oBtnClose.onmousedown=function (){
- oMsgbox.style.display='none';
- };
- oMsgbox.style.left=(document.body.scrollLeft||document.documentElement.scrollLeft)+(document.documentElement.clientWidth-oMsgbox.offsetWidth)/2+'px';
- oMsgbox.style.top=(document.body.scrollTop||document.documentElement.scrollTop)+(document.documentElement.clientHeight-oMsgbox.offsetHeight)/2+'px';
- new PerfectDraging
- (
- oH2Title,
- function (){
- return {x:oMsgbox.offsetLeft, y:oMsgbox.offsetTop};
- },
- function (x, y)
- {
- var iSTop=document.body.scrollTop || document.documentElement.scrollTop;
- if(x<0){
- x=0;
- }
- else if(x+oMsgbox.offsetWidth>document.documentElement.clientWidth){
- x=document.body.clientWidth-oMsgbox.offsetWidth;
- }
- if(y<iSTop){
- y=iSTop;
- }
- else if(y+oMsgbox.offsetHeight>document.documentElement.clientHeight+iSTop){
- y=document.documentElement.clientHeight-oMsgbox.offsetHeight+iSTop;
- }
- oMsgbox.style.left=x+'px';
- oMsgbox.style.top=y+'px';
- }
- );
- /* Download by http://www.codefans.net */
- for(i=0;i<aDiv.length;i++){
- fnDrag=null;
- switch(aDiv[i].className){
- case 'drag':
- fnDrag=doBothDrag;
- oDragBoth=aDiv[i];
- break;
- case 'bar_t':
- fnDrag=doTDrag;
- oDragT=aDiv[i];
- break;
- case 'bar_r':
- fnDrag=doRDrag;
- oDragR=aDiv[i];
- break;
- case 'bar_b':
- fnDrag=doBDrag;
- oDragB=aDiv[i];
- break;
- case 'bar_l':
- fnDrag=doLDrag;
- oDragL=aDiv[i];
- break;
- case 'content':
- oContent=aDiv[i];
- break;
- }
- if(!fnDrag){
- continue;
- }
- new PerfectDraging
- (
- aDiv[i],
- function ()
- {
- g_orgTop=oMsgbox.offsetTop;
- g_orgHeight=oMsgbox.offsetHeight;
- g_orgLeft=oMsgbox.offsetLeft;
- g_orgWidth=oMsgbox.offsetWidth;
- return {x:oMsgbox.offsetWidth, y:oMsgbox.offsetHeight};
- },
- fnDrag
- /*function (x, y)
- {
- fnDrag(x, y);
- }*/
- );
- }
- };
- function doBothDrag(x, y){
- if(x<110){
- x=110;
- }
- oMsgbox.style.width=x-8+'px';
- oDragT.style.width=x+'px';
- oDragB.style.width=x+'px';
- if(y<35){
- y=35;
- }
- oMsgbox.style.height=y-8+'px';
- oDragL.style.height=y+'px';
- oDragR.style.height=y+'px';
- oDragBoth.style.top=y-16+'px';
- }
- function doTDrag(x, y){
- var h=2*g_orgHeight-y;
- if(h<35){
- h=35;
- }
- oMsgbox.style.top=g_orgTop+g_orgHeight-h+'px';
- oMsgbox.style.height=h-8+'px';
- oDragL.style.height=h+2+'px';
- oDragR.style.height=h+2+'px';
- oDragBoth.style.top=h-16+'px';
- }
- function doRDrag(x, y){
- if(x<110){
- x=110;
- }
- oMsgbox.style.width=x-8+'px';
- oDragT.style.width=x+2+'px';
- oDragB.style.width=x+2+'px';
- }
- function doBDrag(x, y){
- if(y<35){
- y=35;
- }
- oMsgbox.style.height=y-8+'px';
- oDragL.style.height=y+2+'px';
- oDragR.style.height=y+2+'px';
- oDragBoth.style.top=y-16+'px';
- }
- function doLDrag(x, y){
- var w=2*g_orgWidth-x;
- if(w<110){
- w=110;
- }
- oMsgbox.style.left=g_orgLeft+g_orgWidth-w+'px';
- oMsgbox.style.width=w-8+'px';
- oDragT.style.width=w+2+'px';
- oDragB.style.width=w+2+'px';
- }
- function PerfectDraging(oElement, fnGetPos, fnOnDrag){
- var obj=this;
- this.oElement=oElement;
- this.fnGetPos=fnGetPos;
- this.fnOnDrag=fnOnDrag;
- this.__oStartOffset__={x:0, y:0};
- this.fnOnMouseUp=function (ev){
- obj.stopDrag(window.event || ev);
- };
- this.fnOnMouseMove=function (ev){
- obj.doDrag(window.event || ev);
- };
- this.oElement.onmousedown=function (ev){
- obj.startDrag(window.event || ev);
- };
- }
- PerfectDraging.prototype.startDrag=function (oEvent){
- var oPos=this.fnGetPos();
- //var x=oEvent.pageX || oEvent.x;
- //var y=oEvent.pageY || oEvent.y;
- var x=oEvent.clientX;
- var y=oEvent.clientY;
- this.__oStartOffset__.x=x-oPos.x;
- this.__oStartOffset__.y=y-oPos.y;
- if(this.oElement.setCapture){
- this.oElement.setCapture();
- thisthis.oElement.onmouseup=this.fnOnMouseUp;
- thisthis.oElement.onmousemove=this.fnOnMouseMove;
- }
- else{
- document.addEventListener("mouseup", this.fnOnMouseUp, true);
- document.addEventListener("mousemove", this.fnOnMouseMove, true);
- window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
- }
- };
- PerfectDraging.prototype.stopDrag=function (oEvent){
- if(this.oElement.releaseCapture){
- this.oElement.releaseCapture();
- this.oElement.onmouseup=null;
- this.oElement.onmousemove=null;
- }
- else{
- document.removeEventListener("mouseup", this.fnOnMouseUp, true);
- document.removeEventListener("mousemove", this.fnOnMouseMove, true);
- window.releaseEvents(Event.MOUSE_MOVE | Event.MOUSE_UP);
- }
- };
- PerfectDraging.prototype.doDrag=function (oEvent){
- //var x=oEvent.pageX || oEvent.x;
- //var y=oEvent.pageY || oEvent.y;
- var x=oEvent.clientX;
- var y=oEvent.clientY;
- this.fnOnDrag(x-this.__oStartOffset__.x, y-this.__oStartOffset__.y);
- };
- //-->
- </script>
- </head>
- <body style="height:2000px;">
- <div id="msgbox" class="drag_layer">
- <h2>
- 标题
- <a href="#" class="close" title="关闭"></a>
- </h2>
- <div class="content">
- 内容
- </div>
- <div class="drag"></div>
- <div class="bar_t"></div>
- <div class="bar_r"></div>
- <div class="bar_b"></div>
- <div class="bar_l"></div>
- </div>
- </body>
- </html>
4、可拖动
此弹出层非常实用,源码分析见:
http://xuqin.blog.51cto.com/5183168/984397
附件:http://down.51cto.com/data/2361341
本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/974957,如需转载请自行联系原作者