Discuz教程网

JS弹出层/窗口源代码收集,很全的

[复制链接]
authicon dly 发表于 2011-1-11 19:54:59 | 显示全部楼层 |阅读模式
JS弹出层/窗口源代码收集,很全的,很好用!
  1. <!DOCTYPE html>
  2. <html>
  3. <head><title>JS弹出层</title>
  4. <style>
  5. body {margin:0px}
  6. #Loading {position:absolute;z-index:10;left:10px;top:10px;border:1px #666666 solid;background:#eeeeee;width:10px;height:10px}
  7. </style>
  8. <script LANGUAGE="JavaScript">
  9. <!--
  10. function $(){return document.getElementById?document.getElementById(arguments[0]):eval(arguments[0]);}
  11. var OverH,OverW,ChangeDesc,ChangeH=50,ChangeW=50;
  12. function OpenDiv(_Dw,_Dh,_Desc) {
  13. $("Loading").innerHTML="";
  14. OverH=_Dh;OverW=_Dw;ChangeDesc=_Desc;
  15. $("Loading").style.display=\'\';
  16. if(_Dw>_Dh){ChangeH=Math.ceil((_Dh-10)/((_Dw-10)/50))}else if(_Dw<_Dh){ChangeW=Math.ceil((_Dw-10)/((_Dh-10)/50))}
  17. $("Loading").style.top=(document.documentElement.clientHeight-10)/2+"px";
  18. $("Loading").style.left=(document.documentElement.clientWidth-10)/2+"px";
  19. OpenNow()
  20. }
  21. var Nw=10,Nh=10;
  22. function OpenNow() {
  23. if (Nw>OverW-ChangeW)ChangeW=2;
  24. if (Nh>OverH-ChangeH)ChangeH=2;
  25. Nw=Nw+ChangeW;Nh=Nh+ChangeH;

  26. if(OverW>Nw||OverH>Nh) {
  27. if(OverW>Nw) {
  28. $("Loading").style.width=Nw+"px";
  29. $("Loading").style.left=(document.documentElement.clientWidth-Nw)/2+"px";
  30. }
  31. if(OverH>Nh) {
  32. $("Loading").style.height=Nh+"px";
  33. $("Loading").style.top=(document.documentElement.clientHeight-Nh)/2+"px"
  34. }
  35. window.setTimeout("OpenNow()",10)
  36. }else{
  37. Nw=10;Nh=10;ChangeH=50;ChangeW=50;
  38. $("Loading").innerHTML=ChangeDesc;
  39. }
  40. }
  41. //-->
  42. </script>
  43. </head>
  44. <body>
  45. <a href="javascript:OpenDiv(500,300,\'层1<br>双击关闭\')">层1 500*300</a><br><br>
  46. <a href="javascript:OpenDiv(500,200,\'层2<br>双击关闭\')">层2 500*200</a><br><br>
  47. <a href="javascript:OpenDiv(200,500,\'层3<br>双击关闭\')">层3 200*500</a><br><br>
  48. <a href="javascript:OpenDiv(500,500,\'层4<br>双击关闭\')">层4 500*500</a>
  49. <div id="Loading" style="display:none" ondblclick="this.style.display=\'none\'"></div>
  50. </body>
  51. </html>
复制代码

可以用鼠标拖动的JS弹出层

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.   <title>简单的测试页面</title>
  6.   <style type="text/css">
  7.   <!--
  8.     html
  9.     {
  10.       height: 100%;
  11.     }
  12.     body
  13.     {
  14.       margin: 0px;
  15.       padding: 0px;
  16.       height: 100%;
  17.     }
  18.     #dt_3
  19.     {
  20.       cursor: pointer;
  21.     }
  22.     div#mbDIV
  23.     {
  24.       position: absolute;
  25.       top: 0px;
  26.       left: 0px;
  27.       width: 100%;
  28.       height: 100%;
  29.       background-color: #AAAAAA;
  30.       z-index: 10;
  31.       filter: alpha(opacity=80);opacity:0.8;
  32.     }
  33.     div#loginDIV
  34.     {
  35.       position: absolute;
  36.       width: 300px;
  37.       height: 150px;
  38.       background-color: #FFFF00;
  39.       z-index: 20;
  40.     }
  41.     div#loginTopDIV
  42.     {
  43.       width: 100%;
  44.       height: 20px;
  45.       background-color: #FF0000;
  46.       cursor: move;
  47.     }
  48.   -->
  49.   </style>
  50.   <script type="text/javascript">
  51.   <!--
  52.     function show(ele)
  53.     {
  54.       eval(ele + ".style.display = \'\'");
  55.     }
  56.     function hidden(ele)
  57.     {
  58.       eval(ele + ".style.display = \'none\'");
  59.     }
  60.   //-->
  61.   </script>
  62. </head>

  63. <body>
  64. <div style="overflow: hidden;">
  65. <h3>请点击 --> 03号拖拉机</h3>
  66. <p id="dt_1">01号拖拉机</p>
  67. <p id="dt_2">02号拖拉机</p>
  68. <p id="dt_3">03号拖拉机</p>
  69. <p id="dt_4">04号拖拉机</p>
  70. <p id="dt_5">05号拖拉机</p>
  71. <p id="dt_6">06号拖拉机</p>
  72. <p id="dt_7">07号拖拉机</p>
  73. <p id="dt_8">08号拖拉机</p>
  74. <p id="dt_9">09号拖拉机</p>
  75. <p id="dt_10">10号拖拉机</p>
  76. <p id="dt_11">11号拖拉机</p>
  77. </div>

  78. <div id="mbDIV" style="display: none;"></div>
  79. <div id="loginDIV" style="top: 200px;left: 300px;display: none;">
  80. <div id="loginTopDIV">Move</div>
  81. <p style="text-align: center;">登陆内容在这里哦</p>
  82. <form action="#" method="get" style="text-align: center;">
  83. <input type="button" value="确定" id="button_1" /> <input type="button" value="取消" id="button_2" />
  84. </form>
  85. </div>

  86. <script type="text/javascript">
  87. <!--
  88. /**
  89. * 这里是乱七八遭信息
  90. * */
  91.   for(var i=1;i<=11;i++)
  92.   {
  93.     eval("var dt_" + i + " = document.getElementById(\'dt_" + i + "\')");
  94.   }
  95.   var mbDIV = document.getElementById("mbDIV");
  96.   var loginDIV = document.getElementById("loginDIV");
  97.   var loginTopDIV = document.getElementById("loginTopDIV");

  98.   document.getElementById("button_1").onclick = function()
  99.                                                 {
  100.                                                   hidden("loginDIV");
  101.                                                   hidden("mbDIV");
  102.                                                 }
  103.   document.getElementById("button_2").onclick = function()
  104.                                                 {
  105.                                                   hidden("loginDIV");
  106.                                                   hidden("mbDIV");
  107.                                                 }
  108.   dt_3.onclick = function()
  109.                  {
  110.                    loginDIV.style.top = "200px";
  111.                    loginDIV.style.left = "300px";
  112.                    show("loginDIV");
  113.                    show("mbDIV")
  114.                  }

  115. /**
  116. *这里写的是拖动信息
  117. * */
  118.     loginTopDIV.onmousedown = Down;
  119.     var tHeight,lWidth;
  120.     function Down(e)
  121.     {
  122.         var event = window.event || e;
  123.         tHeight = event.clientY  - parseInt(loginDIV.style.top.replace(/px/,""));
  124.         lWidth  = event.clientX - parseInt(loginDIV.style.left.replace(/px/,""));
  125.         document.onmousemove = Move;
  126.         document.onmouseup   = Up;
  127.     }
  128.     function Move(e) {
  129.         var event = window.event || e;
  130.         var top = event.clientY - tHeight;
  131.         var left = event.clientX - lWidth;
  132.         //判断 top 和 left 是否超出边界
  133.         top = top < 0 ? 0 : top;
  134.         top = top > document.body.offsetHeight - 150 ? document.body.offsetHeight - 150 : top;
  135.         left = left < 0 ? 0 : left;
  136.         left = left > document.body.offsetWidth - 300 ? document.body.offsetWidth - 300 : left;
  137.         loginDIV.style.top  = top + "px";
  138.         loginDIV.style.left = left +"px";
  139.     }
  140.     function Up() {
  141.         document.onmousemove = null;
  142.     }
  143. //-->
  144. </script>
  145. </body>
  146. </html>
复制代码

一直为JS弹出层的定位问题头疼,现在不用愁了,不同的弹出层可以用CSS来定位,不用在JS给每个弹出层都定同样的位置,具体代码如下:
演示地址:猛击这里
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>JS弹出层</title>
  6. <script language="javascript">
  7. function BOX_show(e)//显示
  8. {
  9.     if(document.getElementById(e)==null)
  10.     {
  11.         return ;
  12.     }
  13.     var selects = document.getElementsByTagName(\'select\');
  14.     for(i = 0; i < selects.length; i++)
  15.     {
  16.         selects[i].style.visibility = "hidden";
  17.     }

  18.     BOX_layout(e);
  19.     window.onresize = function(){BOX_layout(e);} //改变窗体重新调整位置
  20.     window.onscroll = function(){BOX_layout(e);} //滚动窗体重新调整位置
  21.     document.onkeyup = function(event)
  22.     {
  23.         var evt = window.event || event;
  24.         var code = evt.keyCode?evt.keyCode : evt.which;
  25.         //alert(code);

  26.         if(code == 27)
  27.         {
  28.             BOX_remove(e);
  29.         }
  30.     }

  31. }

  32. function BOX_remove(e)//移除
  33. {
  34.     window.onscroll = null;
  35.     window.onresize = null;
  36.     document.getElementById(\'BOX_overlay\').style.display="none";
  37.     document.getElementById(e).style.display="none";

  38.     var selects = document.getElementsByTagName(\'select\');
  39.     for(i = 0; i < selects.length; i++)
  40.     {
  41.         selects[i].style.visibility = "visible";
  42.     }
  43. }

  44. function BOX_layout(e)//调整位置
  45. {
  46.     var a = document.getElementById(e);

  47.     if (document.getElementById(\'BOX_overlay\')==null)//判断是否新建遮掩层
  48.     {

  49.         var overlay = document.createElement("div");
  50.         overlay.setAttribute(\'id\',\'BOX_overlay\');

  51.         //overlay.onclick=function(){BOX_remove(e);};
  52.         //a.parentNode.appendChild(overlay);
  53.         document.body.appendChild(overlay);
  54.     }

  55.     document.getElementById(\'BOX_overlay\').ondblclick=function(){BOX_remove(e);};
  56.     //取客户端左上坐标,宽,高
  57.     var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
  58.     var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

  59.     var clientWidth;
  60.     if (window.innerWidth)
  61.     {
  62.         clientWidth = window.innerWidth;
  63.        // clientWidth = ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
  64.     }
  65.     else
  66.     {
  67.         clientWidth = document.documentElement.clientWidth;
  68.     }

  69.     var clientHeight;
  70.     if (window.innerHeight)
  71.     {
  72.         clientHeight = window.innerHeight;
  73.         //clientHeight = ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
  74.     }
  75.     else
  76.     {
  77.         clientHeight = document.documentElement.clientHeight;
  78.     }

  79.     var bo = document.getElementById(\'BOX_overlay\');
  80.     bo.style.left = scrollLeft+\'px\';
  81.     bo.style.top = scrollTop+\'px\';
  82.     bo.style.width = clientWidth+\'px\';
  83.     bo.style.height = clientHeight+\'px\';
  84.     bo.style.display="";
  85.     //Popup窗口定位
  86.     a.style.position = \'absolute\';
  87.     a.style.zIndex=999;
  88.     a.style.display="";
  89.     //a.style.left = scrollLeft+((clientWidth-a.offsetWidth)/2)+\'px\';
  90.     //a.style.top = scrollTop+((clientHeight-a.offsetHeight)/2)+\'px\';
  91. }

  92. function HiddenButton(e)
  93. {
  94.     e.style.visibility=\'hidden\';
  95.     e.coolcodeviousSibling.style.visibility=\'visible\'
  96. }
  97. </script>
  98. <style type="text/css">

  99. body{}

  100. #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;}

  101. </style>
  102. </head>

  103. <body>
  104. <p onClick="BOX_show(\'messdiv\')"  style="cursor:pointer;">点我就出来</p>
  105. <div id="messdiv" style="position:absolute; display:none; top:135px; left:350px; width:560px; padding:10px 20px 0; background:#f8e2a0;text-align:left; border:2px solid #5d3f0c;z-index:999; font-size:12px;">
  106.         <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>
  107. <br /><br /><br /><br />这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容<br /><br /><br /><br /><br /><br /><br /><br />
  108.                    <div style="width:176px;padding:0 120px;overflow:hidden;margin:10px auto 20px;">
  109.                 <a title="关闭窗口" href="javascript:void(0)" onclick="BOX_remove(\'messdiv\')" target="_self"  style=" background:#fff;float:left; width:70px;height:20px;padding:5px 0 0 10px;color:#feeba9;margin-right:10px;"><span style="color:#ffd014;">关闭窗口</span></a>

  110.                 <a title="收藏页面" href="javascript:void(0)" onClick="addbookmark()" target="_self" style=" background:#fff;float:left;width:70px;height:20px;padding:5px 0 0 10px;color:#feeba9;"><span style="color:#ffd014;">收藏页面</span></a>
  111.             </div>
  112.         <div style="clear:both;"></div>
  113. </div>
  114. <div id="BOX_overlay"></div>
  115. <script type="text/javascript">
  116. //添加收藏夹
  117. function addbookmark()
  118. {
  119.     var nome_sito = "1314学习网";
  120.        var url_sito = "https://www.discuz.1314study.com/";
  121.        if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt
  122.            (navigator.appVersion) >= 4))
  123.            window.external.AddFavorite(url_sito, nome_sito);
  124.        else if (navigator.appName == "Netscape")
  125.            window.sidebar.addPanel(nome_sito, url_sito, \'\');
  126.        else
  127.            alert("Sorry!Cann\'t Add this site to your favorite!.");}
  128. </script>
  129. </body>
  130. </html>
复制代码







上一篇:PHP生成静态页面详解-PHP教程,PHP应用
下一篇:mysql 基本命令
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 17:42

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表