JQUERY+CSS3+HTML5 implementation pop-up Layer effect instance code (source download) _jquery

Source: Internet
Author: User
Tags jquery library

The pop-up layer is used to present detailed information to the user and is highly interactive. The pop-up layer has dialog boxes, modal windows, and so on, I call them the pop-up layer, and so do my colleagues. Generally we like to use the more mature pop-up layer plug-ins such as FancyBox, but in this article, I will first discard plug-ins, to show you how to use JQUERY+CSS3+HTML5 implementation of the pop-up layer.

Effect Display source Download

We completely use Html5+css3+jquery to implement a basic pop-up effect, so we can modify the pop-up layer appearance style and even the JS method call in the example. The pop-up layer effect we end up with should be responsive, that is, a pop-up layer that can be well displayed on both the desktop PC and the mobile side, and is compatible with modern browsers. In the demo, I used the bootstrap style, of course, you can also write a style to complete the page layout.

A basic pop-up layer should meet the following requirements:

1, click the button/chain contact hair pop-up layer, the pop-up layer should have a translucent mask layer;

2, click on the pop-up layer of the Close button, Cancel button or mask layer will close the hidden pop-up layer;

3, use the ESC key can also close the pop-up layer;

4, it is responsive, and compatible with modern mainstream browsers.

Html

First we place a link to trigger the pop-up layer on the page, or it can be a button, note that we give it a Data-show-layer attribute, which corresponds to the ID of the pop-up layer, which means that the pop-up layer is associated with the Data-show-layer.

 
 

Obviously, the ID of the pop-up layer associated with the above link is hw-layer. Well, choose us to prepare the HTML code for the pop-up layer hw-layer.

<div class= "Hw-overlay" id= "Hw-layer" > 
<div class= "Hw-layer-wrap" > 
<span class= "Glyphicon" Glyphicon-remove hwlayer-close "></span> 
<div class=" Row "> 
<div class=" Col-md-3 col-sm-12 Hw-icon "> 
<i class=" Glyphicon glyphicon-info-sign "></i> 
</div> 
<div class=" Col-md-9 col-sm-12 "> 
 
 

The outermost layer of our pop-up layers, the mask layer. Hw-overlay, we'll use CSS to control it as a translucent layer that separates the pop-up layer from the page body content. R then in the. Hw-layer-wrap body layer, we can set the content of the pop-up layer, of course, the content by you, we combine the content of the instance pop-up layer is a confirmation dialog box, similar to Window confirm (). The content uses the bootstrap col-* grid layout and the Glyphicon font icon. In fact, the most important thing is to mask the layer and the body layer, the content can be customized according to the requirements of the project, because it may be a form, or can be a plain text description of the content.

Css

CSS is also the key part, we first set the mask layer, it should be invisible by default, position fixed, and is covered the entire page, it is translucent, we have set the background black, transparency 0.3, such as Background-color:rgba ( 0,0,0,0.3). And then the body layer. Hw-layer-wrap, we set its width and calculate its position. Width We can preset a value, height due to the content of how many uncertain, here we do not have to set a specific value, in the next JS section will be highly processed, and then set the center and shadow and other effects of the border. For horizontal and vertical centering settings, see the Helloweba article: How to center a div horizontally and vertically. The content style inside the pop-up layer can be set freely, and finally, media query is used to set the pop-up layer in the small screen to center the problem.

 

Javascript

We use jquery to handle triggering the pop-up layer and to turn off the pop-up layer effect, so we need to preload the jquery library. Showlayer (ID) is a custom function that shows the pop-up layer. When the click of the button or link calls the Showlayer (ID) function, it will show the effect of the display, and calculate the height of the ejection layer distance, so that the pop-up body layer horizontal and vertical orientation of the center. and the function Hidelayer () is to hide the pop-up layer, fadeout () or hide () can be implemented. Finally, the code that closes the pop-up layer when the mask layer is triggered and the ESC button is used to close the pop-up layer.

$ (function () { 
//Show layer 
function Showlayer (id) { 
var layer = $ (' # ' +id), 
layerwrap = Layer.find ('. Hw-layer-wrap '); 
Layer.fadein (); 
Screen Center 
layerwrap.css ({ 
' margin-top ':-layerwrap.outerheight ()/2 
}); 
} 
Hidden layer 
Function Hidelayer () { 
$ ('. Hw-overlay '). fadeout (); 
} 
$ ('. Hwlayer-ok,.hwlayer-cancel,.hwlayer-close '). On (' click ', Function () { 
hidelayer (); 
}); 
Trigger pop-up layer 
$ ('. Show-layer '). On (' click ', Function () { 
var Layerid = $ (this). Data (' Show-layer '); 
Showlayer (Layerid); 
}); 
Click or touch the translucent mask layer outside the pop-up layer, and close the pop-up layer 
$ ('. Hw-overlay '). On (' click ', Function (event) { 
if (Event.target = = this) { 
Hidelayer (); 
} 
); 
Press ESC to close the pop-up layer 
$ (document). KeyUp (function (event) { 
if (Event.keycode = =) { 
hidelayer (); 
} 
}); 

Actually here, a basic pop-up layer effect has been done. We're just doing a basic pop-up layer here, and you can continue to extend the code. We have seen a lot of pop-up layer plug-ins, many are dynamic direct operation of the DOM, that is, through the JS code First createelement, and then in the content append to the body, this way if it is frequent operation of the DOM will consume a certain performance, so from the performance point of view, I recommend using the pop-up layer method provided in this article.

OK, next we will encapsulate the code of the pop-up layer into a simple jquery plug-in to call in the form of Plug-ins to meet the needs of a variety of different pop-up layer effects, please pay attention.

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.