Js click to close the layer outside the pop-up layer

Source: Internet
Author: User

For today's work, we need to create a pop-up layer. Then, we can not only click the close button to close the layer, but also click anywhere outside the pop-up layer to close the pop-up layer.

We often encounter this effect during web Front-end development, that is, we click an element to make the layer appear, and click a local layer outside the layer to hide it, in the ordinary development process, Xiao Bian also encountered such a problem and obtained a better method through his own practice. I will tell you today, in order to make everyone clear, I will directly describe the instance.

1. knowledge to be used

1. Event bubbling

2.event object (e.tar get and e. srcElement)

Ii. Instance

Html code:

The Code is as follows: Copy code

<Div id = "div1"> </div>
<Input type = "button" value = "click" id = "btn"/>

Js Code:

The Code is as follows: Copy code
Window. onload = function ()
{
Var oDiv = document. getElementById ("div1 ″);
Var oBtn = document. getElementById ("btn ");
Document. onclick = function (ev)
{
Var e = ev | event;
Var targetcmde.tar get | e. srcElement;
If(e.tar get. id! = 'Did1') // event object
{
ODiv. style. display = "none ";
}
}
OBtn. onclick = function (ev)
{
Var e = ev | event;
ODiv. style. display = "block ";
If (e & e. stopPropagation) {// prevents bubbling
E. stopPropagation ();
} Else {
Window. event. cancelBubble = true;
}
}
}

Example 2

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">
<Html xmlns = http://www.bKjia. c0m>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> click the text pop-up layer </title>
<Style type = "text/css">
<! --
* {Font-size: 12px; font-family: Verdana, Geneva, sans-serif; line-height: 14px}
A {color: #039}
A: hover {color: # f60}
. Pop {position: absolute; left: 40%; top: 40%; width: 300px; height: 100px; background: # eee; border: 1px solid # ccc}
. Pop_head {position: relative; height: 20px; background: # ccc}
. Pop_head a {position: absolute; right: 8px; line-height: 20px; color: #000; text-decoration: none}
. Pop_head a: hover {color: # f60; text-decoration: none}
. Pop_body {padding: 8px}
-->
</Style>
</Head>
<Body>
<! -- First set a layer: -->
<Div id = "pop" class = "pop" style = "display: none">
<Div class = "pop_head"> <a href = "javascript: void (0);" onclick = "hide ()"> close </a> </div>
<Div class = "pop_body"> thank you for choosing ...... </Div>
</Div>
<! -- The button for the pop-up layer: -->
<A href = "javascript: void (0);" onclick = "show ();"> pop-up button </a>
<Script type = "text/javascript">
Var EX = {
AddEvent: function (k, v ){
Var me = this;
If (me. addEventListener)
Me. addEventListener (k, v, false );
Else if (me. attachEvent)
Me. attachEvent ("on" + k, v );
Else
Me ["on" + k] = v;
},
RemoveEvent: function (k, v ){
Var me = this;
If (me. removeEventListener)
Me. removeEventListener (k, v, false );
Else if (me. detachEvent)
Me. detachEvent ("on" + k, v );
Else
Me ["on" + k] = null;
},
Stop: function (evt ){
Evt = evt | window. event;
Evt. stopPropagation? Evt. stopPropagation (): evt. cancelBubble = true;
}
};
Document. getElementById ('pop'). onclick = EX. stop;
Var url = '#';
Function show (){
Var o = document. getElementById ('pop ');
O. style. display = "";
SetTimeout (function () {EX. addEvent. call (document, 'click', hide );});
}
Function hide (){
Var o = document. getElementById ('pop ');
O. style. display = "none ";
EX. removeEvent. call (document, 'click', hide );
}
</Script>
</Body>
</Html>

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.