Javascript pop-up layer component (upgraded version)

Source: Internet
Author: User

This time, the organizational structure of the original code is used to re-enhance the function. Currently, there are two other small issues. The first one is that the custom pop-up layer in ie6 will not be able to cover the select, currently, it has not been added to the component. You can add an ifame to the custom div to mask it. the pop-up layer of the component can be hidden. The second problem is that the browser cannot automatically follow the browser window because it is absolutely positioned. You will know when you try it. Of course there are certainly many problems, but I think these two are important. I will list them for now and fix them later.

The following code contains comments and can be run directly.
Online Demo http://demo.jb51.net/js/2011/js_popup_up/index.htm
Copy codeThe Code is as follows:
<! DOCTYPE>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> lock page </title>
<Meta name = "author" content = "www.planeart.cn"/>
<Link rel = "stylesheet" type = "text/css" href = "style/css/j_dialog.css"/>
<Style>
Body {padding: 0; margin: 0; font: 12px/1.5 Tahoma, Helvetica, Arial, sans-serif ;}
Body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td {margin: 0; padding: 0 ;}
Button, input, select, textarea {font: 12px/1.5 tahoma, arial, simsun, sans-serif ;}
H1, h2, h3, h4, h5, h6 {font-size: 100%; font-weight: normal ;}
Address, cite, dfn, em, var {font-style: normal ;}
Code, kbd, pre, samp {font-family: courier new, courier, monospace ;}
Small {font-size: 12px ;}
Ul, ol {list-style: none ;}
Sup {vertical-align: text-top ;}
Sub {vertical-align: text-bottom ;}
Legend {color: #000 ;}
Fieldset, img {border: 0 ;}
Button, input, select, textarea {font-size: 100% ;}
Table {border-collapse: collapse; border-spacing: 0 ;}
. Clear {clear: both ;}
Html {overflow:-moz-scrollbars-vertical ;}
A {text-decoration: none ;}
A: hover {text-decoration: underline ;}

# PageOverlay {overflow: hidden; display: none; position: fixed; top: 0; left: 0; z-index: 1987; width: 100%; height: 100%; background: #000; filter: alpha (opacity = 70); opacity: 0.7 ;}
. PageDialog {border: 4px solid #999999; display: none; position: fixed; top: 40%; left: 50%; z-index: 1988; background: # fff ;}
. PageDialog h3 {padding-left: 10px; overflow: hidden; font-size: 14px; font-weight: normal; height: 25px; line-height: 25px; background: #666; color: # fff; zoom: 1 ;}
. PageDialog h3 a {float: right; color: # ddd; display: inline; font-weight: bold; width: 20px; text-align: center; margin-right: 5px ;}
. PageDialog h3 a: hover {text-decoration: none; color: # fff ;}
. PageDialog span {margin: 10px; display: block ;}
. PageDialog. confirm ,. pageDialog. concel {display: inline-block; background: # ccc url (.. /images/DialogBtn.png) no-repeat; width: 45px; height: 25px; text-align: center; line-height: 25px; font-weight: bold; margin-right: 10px ;}
. PageDialog. confirm: hover,. pageDialog. concel: hover {text-decoration: none ;}
. PageDialog. confirm {background-position: 0 0; color: #000 ;}
. PageDialog. concel {background-position:-45px 1px; color: #000; display: none ;}
. Oprate {margin: 20px 0 10px 0; text-align: center ;}
# Test li {display: inline ;}
</Style>
<Script type = "text/javascript">
Function d_log (contents ){
If (! (This instanceof d_log ))
Return new d_log (contents );
This. title = contents & contents. title | "system prompt"; // default system prompt
This. con = contents & contents. con | ""; // the pop-up content is blank by default.
This. wrap = contents & contents. wrapId; // whether it is a custom pop-up layer
This. confirm = contents & contents. confirm; // whether the confirmation button is required
This. wraphide = contents & contents. wraphide |! 1; // whether to display the background Mask Layer
This. pos = contents & contents. pos; // the left and right sides of the pos are located.
If (document. getElementById ('pageoverlay ') {// share a cover layer to avoid redundant labels
This. cover = document. getElementById ('pageoverlay ');
} Else {
This. cover = document. createElement ('div ');
This. cover. id = 'pageoverlay ';
This. cover. style. display = 'none ';
Document. body. appendChild (this. cover );
}
This. dialog = document. createElement ('div '); // dialog box
This. dialog. className = 'pagedialog ';
Document. body. appendChild (this. dialog );
This. init ();
}
D_log.prototype.init = function (){
Var self = this, c_height, l, t;
C_height = document. compatMode! = "BackCompat "? Document.doc umentElement. clientHeight: document. body. clientHeight;
If (self. wrap) {// if a layer is displayed, add it to the pop-up layer.
Self. dialog. appendChild (document. getElementById (self. wrap ));
} Else {// If NO, a new pop-up layer is created.
Self. dialog. innerHTML = '<div style = "width: 300px;"> Self. dialog. innerHTML + = self. con + "<p class = 'opprate'> <a href = 'javascript:; 'class = 'Confirm'> OK </a> <a href = 'javascript :; 'class = 'cmd'> cancel </a> </p> </div> ";
Var _ p = self. dialog. getElementsByTagName ('P') [0];
If (self. confirm)
_ P. lastChild. style. display = 'inline-Block ';
AddEvent (_ p. getElementsByTagName ('A') [0], 'click', function (){
Self. close ();
If (self. confirm) eval (self. confirm + '()');
});
AddEvent (_ p. getElementsByTagName ('A') [1], 'click', function (){
Self. close ();
});
AddEvent (self. dialog. getElementsByTagName ('A') [0], 'click', function (){
Self. close ();
});
}
Switch (self. pos ){
Case 'left-top ':
L = 0;
T = 0;
Break;
Case 'left-bottom ':
L = 0;
C_height-parseInt (getSize (self. dialog). height );
Break;
Case 'right-top ':
L = document. body. clientWidth-parseInt (getSize (self. dialog). width );
T = 0;
Break;
Case 'right-bottom ':
L = document. body. clientWidth-parseInt (getSize (self. dialog). width );
C_height-parseInt (getSize (self. dialog). height );
Break;
Default:
L = (document. body. clientWidth-parseInt (getSize (self. dialog). width)/2;
T = (c_height-parseInt (getSize (self. dialog). height)/2;
}
Self. dialog. style. left = l + 'px ';
Self. dialog. style. top = t + 'px ';
If (! Window. XMLHttpRequest ){
Var body, clone, cover = self. cover;
Var iframe = '<iframe width = "100%" height = "100%" frameborder = "0" scrolling = "no" style = "z-index: 1000; position: absolute; left: 0pt; top: 0pt; filter: alpha (opacity = 0); "> </iframe> ';
Self. dialog. style. position = 'absolute ';
Try {
Document.exe cCommand ("BackgroundImageCache", false, true );
} Catch (e ){}
(Function (){
Body = document. body;
If (body. currentStyle. backgroundAttachment! = "Fixed "){
If (body. parentNode. currentStyle. backgroundImage = "none "){
Body. parentNode. runtimeStyle. backgroundRepeat = "no-repeat ";
Body. parentNode. runtimeStyle. backgroundImage = "url (about: blank )";
}
Body. style. height = '20180101 ';
}
Self. layer = document. createElement ("<div style = 'position: absolute; z-index: 1990; border: 0; padding: 0; margin: 0; overflow: hidden; background: transparent; top: expression((document).doc umentElement. scrollTop); left: expression(document).doc umentElement. scrollLeft); width: expression(document).doc umentElement. clientWidth); height: expression(document).doc umentElement. clientHeight); display: none; '> ");
})();
Clone = self. dialog. cloneNode (true );
Document. body. removeChild (self. dialog );
Self. layer. appendChild (clone );
Self. dialog = clone;
If (self. layer. parentNode! = Body)
Body. insertBefore (self. layer, body. childNodes [0]);
// Self. dialog. innerHTML + = iframe;
Cover. innerHTML = iframe;
Cover.style.css Text = 'position: absolute; left: expression((document).doc umentElement. scrollLeft); top: expression(document).doc umentElement. scrollTop); width: expression(document).doc umentElement. clientWidth); height: expression(document).doc umentElement. clientHeight );';
}
}
D_log.prototype.open = function (){
If (this. layer)
This. layer. style. display = 'block ';
This. dialog. style. display = 'block ';
If (! This. wraphide)
This. cover. style. display = 'block ';
}
D_log.prototype.close = function (){
If (this. layer)
This. layer. style. display = 'none ';
This. dialog. style. display = 'none ';
This. cover. style. display = 'none ';
}
Function getSize (elem) {// obtain the width and height of an element, including the hidden element's
Var width = elem. offsetWidth, height = elem. offsetHeight;
If (! Width &&! Height ){
Var style = elem. style;
Var cssShow = "position: absolute; visibility: hidden; display: block; left:-9999px; top:-9999px ;";
Var cssBack = "position:" + style. position + "; visibility:" + style. visibility + "; display:" + style. display + "; left:" + style. left + "; top:" + style. top;
Elem.style.css Text = cssShow;
Width = elem. offsetWidth; height = elem. offsetHeight;
Elem.style.css Text = cssBack;
}
Return {"width": width, "height": height };
}
Function addEvent (el, type, fn) {// bind an event
Var self = this;
If (el. attachEvent ){
El ['E' + type + fn] = fn; // copy element references under IE to point this to the el object instead of the window
El [type + fn] = function () {el ['E' + type + fn] (window. event );}
El. attachEvent ('on' + type, el [type + fn]);
} Else
El. addEventListener (type, fn, false );
}
</Script>
</Head>

<Body onload = "dlg3.open ()">
<Ol id = "test">
<Li id = "par" style = "display: none;"> <a id = 'no1' class = "abc ggl" href = "javascript: dlg1.open (); "> click here to try </a> </li>
<Li> <a href = "javascript: dlg2.open ();"> Custom pop-up layer </a> </li>
<Li> <a href = "javascript: dlg1.open ();"> built-in Modal Dialog Box </a> </li>
<Li> <a href = "javascript: dlg3.open ();"> lower-right advertisement </a> </li>
</Ol>
<Div id = "result"> </div>
<Select> <option label = "1111111111"> abcd111 </option> <option label = "222222222"> abcd222 </option> </select>
<Div id = "owp" style = "width: 300px; height: 200px;"> This is the second test example! <A href = "javascript: dlg2.close ();"> x </a> </div>
<Div id = "owp1" style = "width: 200px; height: 100px;"> lower right corner advertisement <a href = "javascript: dlg3.close (); "> x </a> </div>
<Br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>
</Body>
</Html>
<Script>
// The con parameter is the content in the pop-up box, confirm is the confirmation button, and wraphide displays the mask layer and wrap custom layer id.
Var dlg1 = d_log ({con: '<span> are you sure you want to delete this file? </Span> ', confirm: 'check'}); // automatically generated dialog
Var dlg2 = d_log ({wraphide: 'hide ', wrapId: 'owp'}); // custom dialog
Var dlg3 = d_log ({wraphide: 'hide ', wrapId: 'owp1', pos: 'right-bottom'}); // position of the pos pop-up layer
Var arr = [1, 2, 4, 5, 5, 6, 7, 8];
Function check (){
Alert ('test! ');
}
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.