javascript 弹出浮动层,并锁定当前窗口一

 js代码

--------------------------------------------------------------------------------------------------------------------------------------------

  1. /**
  2.  * SUBMODAL v1.6
  3.  * Used for displaying DHTML only popups instead of using buggy modal windows.
  4.  *
  5.  * By Subimage LLC
  6.  * http://www.subimage.com
  7.  *
  8.  * Contributions by:
  9.  *  Eric Angel - tab index code
  10.  *  Scott - hiding/showing selects for IE users
  11.  *  Todd Huss - inserting modal dynamically and anchor classes
  12.  *
  13.  * Up to date code can be found at http://submodal.googlecode.com
  14.  */
  15. // Popup code
  16. var gPopupMask = null;
  17. var gPopupContainer = null;
  18. var gPopFrame = null;
  19. var gReturnFunc;
  20. var gPopupIsShown = false
  21. var gDefaultPage = "";
  22. var gHideSelects = false;
  23. var gReturnVal = null;
  24. var gTabIndexes = new Array();
  25. // Pre-defined list of tags we want to disable/enable tabbing into
  26. var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");    
  27. // If using Mozilla or Firefox, use Tab-key trap.
  28. if (!document.all) {
  29.     document.onkeypress = keyDownHandler;
  30. }
  31. /**
  32.  * Initializes popup code on load.  
  33.  */
  34. function initPopUp() {
  35.     // Add the HTML to the body
  36.     theBody = document.getElementsByTagName('BODY')[0];
  37.     popmask = document.createElement('div');
  38.     popmask.id = 'popupMask';
  39.     popcont = document.createElement('div');
  40.     popcont.id = 'popupContainer';
  41.     popcont.innerHTML = '' +
  42.         '<div id="popupInner">' +
  43.             '<div id="popupTitleBar">' +
  44.                 '<div id="popupTitle"></div>' +
  45.                 '<div id="popupControls">' + 
  46.                     '<img src="/images/album/close.gif" οnclick="hidePopWin(false);" id="popCloseBox" />' +
  47.                 '</div>' +
  48.             '</div>' +
  49.             '<iframe src="'+ gDefaultPage +'" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
  50.         '</div>';
  51.     theBody.appendChild(popmask);
  52.     theBody.appendChild(popcont);
  53.     
  54.     gPopupMask = document.getElementById("popupMask");
  55.     gPopupContainer = document.getElementById("popupContainer");
  56.     gPopFrame = document.getElementById("popupFrame");  
  57.     
  58.     // check to see if this is IE version 6 or lower. hide select boxes if so
  59.     // maybe they'll fix this in version 7?
  60.     var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
  61.     if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
  62.         gHideSelects = true;
  63.     }
  64.     
  65.     // Add onclick handlers to 'a' elements of class submodal or submodal-width-height
  66.     var elms = document.getElementsByTagName('a');
  67.     for (i = 0; i < elms.length; i++) {
  68.         if (elms[i].className.indexOf("submodal") == 0) { 
  69.             // var onclick = 'function (){showPopWin(/''+elms[i].href+'/','+width+', '+height+', null);return false;};';
  70.             // elms[i].onclick = eval(onclick);
  71.             elms[i].onclick = function(){
  72.                 // default width and height
  73.                 var width = 400;
  74.                 var height = 200;
  75.                 // Parse out optional width and height from className
  76.                 params = this.className.split('-');
  77.                 if (params.length == 3) {
  78.                     width = parseInt(params[1]);
  79.                     height = parseInt(params[2]);
  80.                 }
  81.                 showPopWin(this.href,width,height,null); return false;
  82.             }
  83.         }
  84.     }
  85. }
  86. addEvent(window, "load", initPopUp);
  87.  /**
  88.     * @argument width - int in pixels
  89.     * @argument height - int in pixels
  90.     * @argument url - url to display
  91.     * @argument returnFunc - function to call when returning true from the window.
  92.     * @argument showCloseBox - show the close box - default true
  93.     */
  94. function showPopWin(url, width, height, returnFunc, showCloseBox) {
  95.     // show or hide the window close widget
  96.     if (showCloseBox == null || showCloseBox == true) {
  97.         document.getElementById("popCloseBox").style.display = "block";
  98.     } else {
  99.         document.getElementById("popCloseBox").style.display = "none";
  100.     }
  101.     gPopupIsShown = true;
  102.     disableTabIndexes();
  103.     gPopupMask.style.display = "block";
  104.     gPopupContainer.style.display = "block";
  105.     // calculate where to place the window on screen
  106.     centerPopWin(width, height);
  107.     
  108.     var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
  109.     gPopupContainer.style.width = width + "px";
  110.     gPopupContainer.style.height = (height+titleBarHeight) + "px";
  111.     
  112.     setMaskSize();
  113.     // need to set the width of the iframe to the title bar width because of the dropshadow
  114.     // some oddness was occuring and causing the frame to poke outside the border in IE6
  115.     gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
  116.     gPopFrame.style.height = (height) + "px";
  117.     
  118.     // set the url
  119.     gPopFrame.src = url;
  120.     
  121.     gReturnFunc = returnFunc;
  122.     // for IE
  123.     if (gHideSelects == true) {
  124.         hideSelectBoxes();
  125.     }
  126.     
  127.     window.setTimeout("setPopTitle();", 600);
  128. }
  129. //
  130. var gi = 0;
  131. function centerPopWin(width, height) {
  132.     if (gPopupIsShown == true) {
  133.         if (width == null || isNaN(width)) {
  134.             width = gPopupContainer.offsetWidth;
  135.         }
  136.         if (height == null) {
  137.             height = gPopupContainer.offsetHeight;
  138.         }
  139.         
  140.         //var theBody = document.documentElement;
  141.         var theBody = document.getElementsByTagName("BODY")[0];
  142.         //theBody.style.overflow = "hidden";
  143.         var scTop = parseInt(getScrollTop(),10);
  144.         var scLeft = parseInt(theBody.scrollLeft,10);
  145.     
  146.         setMaskSize();
  147.         
  148.         //window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
  149.         
  150.         var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
  151.         
  152.         var fullHeight = getViewportHeight();
  153.         var fullWidth = getViewportWidth();
  154.         
  155.         gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
  156.         gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
  157.         //alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
  158.     }
  159. }
  160. addEvent(window, "resize", centerPopWin);
  161. addEvent(window, "scroll", centerPopWin);
  162. window.onscroll = centerPopWin;
  163. /**
  164.  * Sets the size of the popup mask.
  165.  *
  166.  */
  167. function setMaskSize() {
  168.     var theBody = document.getElementsByTagName("BODY")[0];
  169.             
  170.     var fullHeight = getViewportHeight();
  171.     var fullWidth = getViewportWidth();
  172.     
  173.     // Determine what's bigger, scrollHeight or fullHeight / width
  174.     if (fullHeight > theBody.scrollHeight) {
  175.         popHeight = fullHeight;
  176.     } else {
  177.         popHeight = theBody.scrollHeight;
  178.     }
  179.     
  180.     if (fullWidth > theBody.scrollWidth) {
  181.         popWidth = fullWidth;
  182.     } else {
  183.         popWidth = theBody.scrollWidth;
  184.     }
  185.     
  186.     gPopupMask.style.height = popHeight + "px";
  187.     gPopupMask.style.width = popWidth + "px";
  188. }
  189. /**
  190.  * @argument callReturnFunc - bool - determines if we call the return function specified
  191.  * @argument returnVal - anything - return value 
  192.  */
  193. function hidePopWin(callReturnFunc) {
  194.     gPopupIsShown = false;
  195.     var theBody = document.getElementsByTagName("BODY")[0];
  196.     theBody.style.overflow = "";
  197.     restoreTabIndexes();
  198.     if (gPopupMask == null) {
  199.         return;
  200.     }
  201.     gPopupMask.style.display = "none";
  202.     gPopupContainer.style.display = "none";
  203.     if (callReturnFunc == true && gReturnFunc != null) {
  204.         // Set the return code to run in a timeout.
  205.         // Was having issues using with an Ajax.Request();
  206.         gReturnVal = window.frames["popupFrame"].returnVal;
  207.         window.setTimeout('gReturnFunc(gReturnVal);', 1);
  208.     }
  209.     gPopFrame.src = gDefaultPage;
  210.     // display all select boxes
  211.     if (gHideSelects == true) {
  212.         displaySelectBoxes();
  213.     }
  214. }
  215. /**
  216.  * Sets the popup title based on the title of the html document it contains.
  217.  * Uses a timeout to keep checking until the title is valid.
  218.  */
  219. function setPopTitle() {
  220.     return;
  221.     if (window.frames["popupFrame"].document.title == null) {
  222.         window.setTimeout("setPopTitle();", 10);
  223.     } else {
  224.         document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
  225.     }
  226. }
  227. // Tab key trap. iff popup is shown and key was [TAB], suppress it.
  228. // @argument e - event - keyboard event that caused this function to be called.
  229. function keyDownHandler(e) {
  230.     if (gPopupIsShown && e.keyCode == 9)  return false;
  231. }
  232. // For IE.  Go through predefined tags and disable tabbing into them.
  233. function disableTabIndexes() {
  234.     if (document.all) {
  235.         var i = 0;
  236.         for (var j = 0; j < gTabbableTags.length; j++) {
  237.             var tagElements = document.getElementsByTagName(gTabbableTags[j]);
  238.             for (var k = 0 ; k < tagElements.length; k++) {
  239.                 gTabIndexes[i] = tagElements[k].tabIndex;
  240.                 tagElements[k].tabIndex="-1";
  241.                 i++;
  242.             }
  243.         }
  244.     }
  245. }
  246. // For IE. Restore tab-indexes.
  247. function restoreTabIndexes() {
  248.     if (document.all) {
  249.         var i = 0;
  250.         for (var j = 0; j < gTabbableTags.length; j++) {
  251.             var tagElements = document.getElementsByTagName(gTabbableTags[j]);
  252.             for (var k = 0 ; k < tagElements.length; k++) {
  253.                 tagElements[k].tabIndex = gTabIndexes[i];
  254.                 tagElements[k].tabEnabled = true;
  255.                 i++;
  256.             }
  257.         }
  258.     }
  259. }
  260. /**
  261.  * Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
  262.  * IE has a problem with wanted select form tags to always be the topmost z-index or layer
  263.  *
  264.  * Thanks for the code Scott!
  265.  */
  266. function hideSelectBoxes() {
  267.   var x = document.getElementsByTagName("SELECT");
  268.   for (i=0;x && i < x.length; i++) {
  269.     x[i].style.visibility = "hidden";
  270.   }
  271. }
  272. /**
  273.  * Makes all drop down form select boxes on the screen visible so they do not 
  274.  * reappear after the dialog is closed.
  275.  * 
  276.  * IE has a problem with wanting select form tags to always be the 
  277.  * topmost z-index or layer.
  278.  */
  279. function displaySelectBoxes() {
  280.   var x = document.getElementsByTagName("SELECT");
  281.   for (i=0;x && i < x.length; i++){
  282.     x[i].style.visibility = "visible";
  283.   }
  284. }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值