JS implementation Lightbox Effect of pop-up dialog box code

Source: Internet
Author: User
Tags abs unique id visibility

Believe that lightbox such as page pop-up window, we should be browsing the time is very common, not too difficult to achieve, CG today in the last release of ETP project source code in the JS part of the extraction, convenient for everyone to learn and use.
Implementation principle: The use of div floating and layer overlap, the dialog box Z-index set to the highest can be another for the mask layer, as long as the mask layer in the dialog layer, other page elements can be, if you need to beautiful, you may set the translucent effect.


The CSS code is as follows: (Save as MESSAGEBOX.CSS)

The code is as follows Copy Code

/**
* FileName: Messagebox.css
* Author: Li Mingcheng
* Date Created: 2009-12-16
* Current Version: 1.0
* Last modified: 2009-12-16 22:00
*/

/* General css*/

body{
font-size:12px;
padding:0;
margin:0;
}
. right{
Float:right;
}
. red{
color:red;
}
. blue{
Color:blue;
}


#messageBox {
Position:absolute;
border:2px solid #3B5A6E;
Background-color: #FFFFCC;
Display:none;
z-index:9999;
margin:0;
padding:0;
}

/* message box title * *
#messageBox h2{
font-size:12px;
Background:url (top.gif);
border-bottom:1px solid #3B5A6E;
margin:0;
Color: #FFFFFF;
padding:6px 10px 4px 10px;
}

#messageBox a{
Text-decoration:none;
Cursor:pointer;
Color: #FFFFFF;
}

/* Button Properties * *
#messageBox a.button{
Display:inline;
Display:inline-block;!important.
height:24px;
width:60px;
border:1px solid #282828;
Background-color: #989898;
padding:2px;
margin:0 10px;
}

#messageBox p{
Width:auto;
PADDING:6PX 10px;
margin:0;
}

#messageBox P a{
Color: #3B5A6E;
}

#messageBox p.content{
padding-top:12px;
/* Information Content minimum 80px*/
min-height:80px;
}

/* Button List * *
#messageBox p.buttons{
border-top:1px dashed #3B5A6E;
Text-align:center;
margin:0 10px;
}

/* Button List * *
#messageBox p.buttons img{
Cursor:pointer;
}

/* Mask Layer * *
#messageBoxMask {
Position:absolute;
top:0;
left:0;
Background-color: #CCCCCC;
/* The following control transparency effect, IE, ff*/
Filter:alpha (OPACITY=50);
-moz-opacity:0.5; /* Compatible with the old version of FF */
opacity:0.5; /* Mozila compatible */
z-index:1000;
Display:none;
margin:0;
padding:0;
}

JS code is as follows (Save as Messagebox.js)

The code is as follows Copy Code

/**
* Project name: IBMETP Changshu ETP Homepage
* FileName: Messagebox.js
* Author: Li Mingcheng
* Date Created: 2009-12-21
* Current Version: 1.0
* Last modified: 2009-12-23 22:00
* Function Description: Implementation page pop-up message window JS package
*/
/** The following defines the global action function **/
Whether it is IE
var Isie = (document.all)? True:false;

$ ID Selector
var $ = function (ID) {
Return "string" = = typeof ID? document.getElementById (ID): ID;
};

Class object
var Class = {
Create:function () {
return function () {this.initialize.apply (this, arguments);}
}
}

function bindings
var Bind = function (object, fun) {
return function () {
Return Fun.apply (object, arguments);
}
}

For Each loop
var each = function (list, fun) {
for (var i = 0, len = list.length i < len; i++) {Fun (list[i], i);}
};


var MessageBox = Class.create ();
Messagebox.prototype = {
Initialize:function (ID) {
Pop-up box width
This._boxwidth = 400;
Pop-up height
This._boxheight = 180;
Title
This._title = "";
This._titleid = "";
This._message = "";
This._messageid = "";
This._info = "";
This._boxid = ID;
Additional message boxes
This.appendbody ();
Initialize message box
This.initmessagebox ();
},

Appendbody:function () {
To create a dialog container for display
var MessageBox = document.createelement ("div");
Messagebox.id = This._boxid;
Document.body.appendChild (MessageBox);

Create a mask container to display
var messageboxmask = document.createelement ("div");
Messageboxmask.id = This._boxid + "Mask";
Document.body.appendChild (Messageboxmask);
},

Show:function (title, message) {
A condition that only passes as one parameter
if (!message) {
Warning display
This.alert (title);
}
This._title = title;
this._message = message;
This.popup ();
},

Simulate system alert
Alert:function (message) {
This._title = "Message from page";
this._message = message;
This.popup ();
},

Pop-up window
Popup:function () {
$ (this._titleid). InnerHTML = This._title;
$ (this._messageid). InnerHTML = This._message;
if (Isie) {
$ (THIS._BOXID). Style.pixelwidth = This._boxwidth;
Control height
$ (THIS._BOXID). Style.pixelheight = This._boxheight;
Control minimum width
$ (THIS._BOXID). Style.minheight = This._boxheight;
$ (THIS._BOXID). Style.pixelleft = Math.ceil (document.body.clientwidth-this._boxwidth)/2;
Not yet implemented
$ (THIS._BOXID). Style.pixeltop = Math.ceil (Math.Abs ((document.body.clientheight-this._boxheight)/2)) + Document.body.scrollTop;
Set Mask Properties
$ (This._boxid + "Mask"). Style.pixelwidth = Document.body.clientWidth;
$ (This._boxid + "Mask"). Style.pixelheight = Document.body.clientHeight;
}
else{
$ (THIS._BOXID). Style.width = this._boxwidth + "px";
$ (THIS._BOXID). Style.left = Math.ceil ((document.body.clientwidth-this._boxwidth)/2) + "px";
Not yet implemented
$ (THIS._BOXID). Style.top = Math.ceil (Math.Abs ((document.body.clientheight-this._boxheight)/2)) + Document.body.scrollTop + "px";
Set Mask Properties
$ (This._boxid + "Mask"). Style.width = document.body.clientWidth + "px";
$ (This._boxid + "Mask"). Style.height = document.body.clientHeight + "px";
}

Hide Selection box
This.hiddeselect ();
Show Prompt window
$ (THIS._BOXID). style.display = "block";
Show Mask Layer
$ (This._boxid + "Mask"). Style.display = "block";
},

Close:function (message) {
$ (THIS._BOXID). Style.display = "None";
$ (This._boxid + "Mask"). Style.display = "None";

Restore Selection box
This.showselect ();
},

OK button callback
Btnok:function () {
This.close ();
return true;
},

Cancel Button Callback
Btncancel:function () {
This.close ();
return false;
},

Create a button
Createbutton:function (caption, callback) {
Append button
var button = document.createelement ("a");
button.innerhtml = caption;
Button.classname = "button";
Button.onclick = Bind (this, callback);
return button;
},

The following code is used for the control of the selection box (select) and is suitable for IE6 and the following versions
Hide Selection box
Hiddeselect:function () {
var sels = $ (document). getElementsByTagName ("select");
Each (sels, function (sel) {sel.style.visibility = "hidden";});
},

Show Selection box
Showselect:function () {
var sels = $ (document). getElementsByTagName ("select");
Each (sels, function (sel) {sel.style.visibility = "visible";});
},

Initialize message window
Initmessagebox:function () {
var container = $ (THIS._BOXID);
dialog box title
var msgtop = document.createelement ("H2");
dialog box title content
var msgtitle = document.createelement ("span");
Use time to set a unique ID
Msgtitle.id = "Msgtitle" + New Date (). GetTime (). toString ();
This._titleid = msgtitle.id;
Close button
var msgtopclose = document.createelement ("a");
Msgtopclose.classname= "Right";
Msgtopclose.onclick = Bind (this, this.close);
Msgtopclose.innerhtml= "Close";
Additional
First add the Close button, the right display
Msgtop.appendchild (Msgtopclose);
Msgtop.appendchild (Msgtitle);
Adding content elements
var msgcontent = document.createelement ("P");
Msgcontent.classname = "Content";
Use time to set a unique ID
Msgcontent.id = "Msgcontent" + New Date (). GetTime (). toString ();
This._messageid = msgcontent.id;
Add a button
var msgbuttons = document.createelement ("P");
Msgbuttons.classname = "buttons";

Append OK button
Msgbuttons.appendchild (This.createbutton ("OK", This.btnok));

Append Cancel button
Msgbuttons.appendchild (This.createbutton ("cancellation", This.btncancel));

Append element
Container.appendchild (Msgtop);
Container.appendchild (msgcontent);
Container.appendchild (msgbuttons);
}
}

The following is the complete HTML code

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Testing </title>
<link href= "Messagebox.css" rel= "stylesheet" type= "text/css" media= "screen"/>
<!--page dialog box-->
<script src= "Messagebox.js" type= "Text/javascript" language= "JavaScript" ></script>
<script type= "Text/javascript" language= "JavaScript" >
dialog Box Object
var MsgBox;
function init () {
Initialize MSG
MsgBox = New MessageBox ("MessageBox");
msg = "Welcome to use and test CG Prepared Web Page Dialog JS program: <br/> At present this procedure has been in IE6 above, FF2 the above version debugging through!" ";
Use custom Headers
Msgbox.show ("This is the test title", msg);
Use default headers
Msgbox.alert (msg);
}
</script>
<body onload= "Init ();" >
This is the mask layer, which is the same height as the page content
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/><br/>
<br/>
<br/>

</body>

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.