Javascript pop-up window Div Layer Effect code

Source: Internet
Author: User

In this tutorial, I will use the most popular language and the most conciseCodeDemonstrate how to createThe effect of a javascript pop-up Div window layer.

 

Creating a pop-up Div window may be one of the most common problems in website/Webpage creation. The traditional JavaScript pop-up window is no longer suitable for the current website design philosophy. There are two reasons: first, unfriendly-a stiff pop-up dialog box with a "quit" voice poses a great challenge to the user experience. Second, compatibility is not strong enough-a considerable number of browsers have shielded this JS alert () method. Therefore, a website with good user experience needs a more reasonable solution-using few HTML code, few CSS codes and several lines of JavaScript code are used to simulate the default pop-up window of the browser (that is, the default alert () interface and function are replaced ).

 

 




View demoPopup

Download an instanceDemo RAR

 

Implementation principle:

 

First, place the content in the pop-up box in a special Div layer, and hide it by default (that is, it is initially invisible and can be implemented using CSS ). When a user executes an action, such as clicking a link or moving the cursor over a link, the hidden layer is displayed on the top of all page elements (JS operations are used ). ). In addition, we will set a button in the pop-up Div window to execute the function of closing the window when the user clicks this button.

 

Implementation process:

 

As mentioned above, we first need to create a special Div layer, and then place the content of the pop-up window in this Div layer. Here, we name its ID "popupcontent" to distinguish it from other Div layers.

 
<Div id ="Popupcontent"> This is a div pop-up window! </Div>

 

The CSS modifier code in the pop-up window:

 

Next, let's beautify the CSS for the created Div pop-up layer. The most important parameters are overflow, visibility, and position ). At the same time, I also added a lot of other code for the window effect, but these are only used for beautification, making the window more beautiful. Therefore, the CSS code we finally define is like:

# Popupcontent{
Position:Absolute;
Visibility:Hidden;
Overflow:Hidden;
Border:1px Solid # Ccc;
Background-color:# F9f9f9;
Border:1px Solid #333;
Padding:5px;
}

 

The red part in the above CSS Code shows that the initial default status of the DIV layer is invisible.

 

You can beautify the above Code as needed, but be sure to keep the position, visibility, and overflow attributes.

 

The javascript code is used to trigger and display the pop-up window:

 

This may be the most important and interesting part of this tutorial. We will compileThe two process functions are used to show and hide the above Div pop-up window.. Of course, these two functions contain some subject logic.

 

Logic that needs to be included in sequence in process functions:

 

    • Calculate the position (positioning) of the Javascript pop-up window on the screen );
    • In the pop-up window, add a status bar (or button) to close the window in the open status;
    • The pop-up window is displayed.

 

For the sake of simplicity, the display position we set in this example is top: 200, left: 200. That is, the upper left corner of the browser content box is taken as the coordinates, and the offset is 200px down and 200px to the left.

 

The window size can be set in the parameters of the display function, including two parameters: Window Length and window width.

 

If you need to perform secondary development of the code in this example, you need to pay special attention to getting the DOM object of the DIV layer in the pop-up window, we can use the getelementbyid function below to obtain the DOM object named "popcontent.

VaRPopup = document.Getelementbyid("Popupcontent");

 

After obtaining the DOM object (in the pop-up window), we can modify the relative position and size of the window in JS Code.

Popup.Style.Top="200px"; // The offset between the window and the top of the browser content area


Popup.Style.Left="200px"; // The leftmost offset between the window and the browser content area

Popup.Style.Width= W +"PX"; // The window width


Popup.Style.Height= H +"PX"; // Window height

 

next, add a "close" button to the window, used to close a window when it is enabled. To implement this function perfectly, we first need to declare a global variable to store the content in the pop-up window Div. This is because, if multiple pop-up windows with different content are displayed on a page, you do not need to copy the buttons repeatedly to these Div layers, which simplifies the behavior logic:

If (Basetext =Null)Basetext = Popup.Innerhtml;


Popup.Innerhtml= Basetext +


"<Div id =\"Statusbar\"> <Button onclick =\"Hidepopup ();\"> Close Window
<Button> </div>";

 

The last thing to note is how to locate the "close" button. This is easy to implement. Set the up blank side of the button object (the value of the blank side is slightly smaller than the DIV height of the entire pop-up window ).

 

Now, all the action logic is explained. The complete code of the function is displayed in the pop-up window as follows:

VaR basetext = NULL;

Function showpopup (W, h ){
VaR popup = Document. getelementbyid ("popupcontent ");
Popup. style. Top = "200px ";
Popup. style. Left = "200px ";
Popup. style. width = W + "PX ";
Popup. style. Height = H + "PX ";
If (basetext = NULL) basetext = Popup. innerhtml;
Popup. innerhtml = basetext + "<Div id = \" statusbar \ "> <button onclick = \" hidepopup ();
\ "> Close Window <button> </div> ";
VaR sbar = Document. getelementbyid ("statusbar ");
Sbar. style. margintop = (parseint (H)-40) + "PX ";
Popup. style. Visibility = "visible ";
}

 

Hide the pop-up window:

 

Hiding the pop-up window is quite simple. You only need to first obtain the DOM object of the DIV in the pop-up window, and then set its attribute to "hide.

 
FunctionHidepopup(){
VaRPopup = document.Getelementbyid("Popupcontent");
Popup.Style.Visibility="Hidden";
}

 

Expand the HTML code to achieve the pop-up effect:

 

All we need to do is add the JS function "showpopup ()" on the corresponding event of a link or button.

For example, a window pops up when you move the cursor over a connection:

 
<A href = "#" onmouseover = "showpopup (300,200);"> open popup </a>

 

You need to click a connection to bring up a window:

 
<A href = "#" onclick = "showpopup (300,200);"> open popup </a>
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.