<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>前端思考：专注前端开发，关注用户体验 &#187; 弹出层</title>
	<atom:link href="http://www.artcss.com/archives/tag/%e5%bc%b9%e5%87%ba%e5%b1%82/feed" rel="self" type="application/rss+xml" />
	<link>http://www.artcss.com</link>
	<description>一个像素的距离...</description>
	<lastBuildDate>Sat, 12 Feb 2011 03:34:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>弹出层详解，从简单到复杂</title>
		<link>http://www.artcss.com/archives/477.html</link>
		<comments>http://www.artcss.com/archives/477.html#comments</comments>
		<pubDate>Fri, 25 Dec 2009 11:55:38 +0000</pubDate>
		<dc:creator>Artcss</dc:creator>
				<category><![CDATA[个人随笔]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[弹出层]]></category>

		<guid isPermaLink="false">http://www.artskin.cn/blog/?p=477</guid>
		<description><![CDATA[在工作过程中经常遇到做弹出的层效果，有的需要在元素右下弹出，有的需要弹出在浏览器正中间，有的需要弹出后再拖拽，有的需要背景要变暗，有的需要弹出的层跟随鼠标移动…… 网上此类效果其实很多，有javascript原生的，有基于框架写的,但自己好多时候用不到那么高级的效果，所以就把这些功能都分开来一步一步实现。 其实原理很简单.有两种实现途径:一种是通过元素创建和删除，一种是通过显示和隐藏，其余的具体要做成什么样子，就留给CSS来控制了。下面从最简单的开始（不基于框架） 一、最简单的弹出 用到的javascript代码如下： Download script.js1 2 3 4 5 6 7 8 9 10 11 function show&#40;&#41;&#123; var oShow = document.getElementById&#40;'show'&#41;; oShow.style.display = 'block'; var oClose = document.createElement&#40;&#34;span&#34;&#41;; oClose.innerHTML = &#34;×&#34;; oShow.appendChild&#40;oClose&#41;; oClose.onclick = function&#40;&#41;&#123; oShow.style.display = 'none'; oShow.removeChild&#40;this&#41;; &#125; &#125; 点击下面“运行”直接查看效果 &#60;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&#62; &#60;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&#62; &#60;head&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>在工作过程中经常遇到做弹出的层效果，有的需要在元素右下弹出，有的需要弹出在浏览器正中间，有的需要弹出后再拖拽，有的需要背景要变暗，有的需要弹出的层跟随鼠标移动……</p>
<p>网上此类效果其实很多，有javascript原生的，有基于框架写的,但自己好多时候用不到那么高级的效果，<span id="more-477"></span>所以就把这些功能都分开来一步一步实现。</p>
<p>其实原理很简单.有两种实现途径:一种是通过元素创建和删除，一种是通过显示和隐藏，其余的具体要做成什么样子，就留给CSS来控制了。下面从最简单的开始（不基于框架）</p>
<h4>一、最简单的弹出</h4>
<h5>用到的javascript代码如下：</h5>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left2">Download <a href="http://www.artcss.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=477&amp;download=script.js">script.js</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4776"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p477code6"><pre class="javascript" style="font-family:Verdana;"><span style="color: #003366; font-weight: bold;">function</span> show<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> oShow <span style="color: #339933;">=</span> document.<span style="color: #990000;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'show'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> oClose <span style="color: #339933;">=</span> document.<span style="color: #990000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oClose.<span style="color: #990000;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;×&quot;</span><span style="color: #339933;">;</span>
		oShow.<span style="color: #990000;">appendChild</span><span style="color: #009900;">&#40;</span>oClose<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oClose.<span style="color: #990000;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
			oShow.<span style="color: #990000;">removeChild</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h5>点击下面“运行”直接查看效果</h5>
<div class="runcode">
<p><textarea name="runcode" class="runcode_text" id="runcode_QWMMYN">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;前端思考&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;前端思考&quot; /&gt;
&lt;title&gt;最简单的弹出层：前端思考&lt;/title&gt;
&lt;style&gt;
*{margin:0;padding:0px;}
body{padding:30px;}
#show{width:300px;height:200px;display:none;padding:1px;position:relative;border:1px solid #4c77aa;background:#f2f7fd;margin-left:70px;zoom:1;}
#show h3{background:#4c77aa;color:#fff;font-size:14px;padding:5px;}
#show span{position:absolute;right:3px;top:3px;display:block;cursor:pointer;color:#fff;font-weight:bold;}
#show p{padding:5px;}
#text{font-size:12px;text-indent:2em;line-height:20px;}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	function show(){
		var oShow = document.getElementById('show');
		oShow.style.display = 'block';
		var oClose = document.createElement(&quot;span&quot;);
			oClose.innerHTML = &quot;×&quot;;
			oShow.appendChild(oClose);
			oClose.onclick = function(){
				oShow.style.display = 'none';
				oShow.removeChild(this);
			}
	}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a onclick=&quot;show(); return false&quot; href=&quot;#&quot;&gt;请猛击我&lt;/a&gt;
&lt;div id=&quot;show&quot;&gt;
	&lt;h3&gt;弹出层标题栏&lt;/h3&gt;
	&lt;p id=&quot;text&quot;&gt;
		这里是弹出层内容，内容可以是文字、图片等，可以是iframe传进来，也可以用jQuery的load()传进来。如有什么不合理的或可以改进的，大家可以给我留言，共同学习。
	&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</textarea></p>
<p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_QWMMYN');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_QWMMYN');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<h4>二、弹出层—到浏览器中央</h4>
<h5>用到的javascript代码如下：</h5>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left2">Download <a href="http://www.artcss.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=477&amp;download=script.js">script.js</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4777"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p477code7"><pre class="javascript" style="font-family:Verdana;"><span style="color: #003366; font-weight: bold;">function</span> show<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> iWidth <span style="color: #339933;">=</span> document.<span style="color: #990000;">documentElement</span>.<span style="color: #990000;">clientWidth</span><span style="color: #339933;">;</span>	<span style="color: #999999;">//获取浏览器宽度</span>
	<span style="color: #003366; font-weight: bold;">var</span> iHeight <span style="color: #339933;">=</span> document.<span style="color: #990000;">documentElement</span>.<span style="color: #990000;">clientHeight</span><span style="color: #339933;">;</span>	<span style="color: #999999;">//获取浏览器高度</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> oShow <span style="color: #339933;">=</span> document.<span style="color: #990000;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'show'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
        oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">left</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>iWidth<span style="color: #339933;">-</span><span style="color: #ff0000;">302</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #ff0000;">2</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>	<span style="color: #999999;">//居中横坐标</span>
		oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">top</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>iHeight<span style="color: #339933;">-</span><span style="color: #ff0000;">202</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #ff0000;">2</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>	<span style="color: #999999;">//居中纵坐标</span>
	<span style="color: #003366; font-weight: bold;">var</span> oClose <span style="color: #339933;">=</span> document.<span style="color: #990000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oClose.<span style="color: #990000;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;×&quot;</span><span style="color: #339933;">;</span>
		oShow.<span style="color: #990000;">appendChild</span><span style="color: #009900;">&#40;</span>oClose<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oClose.<span style="color: #990000;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
			oShow.<span style="color: #990000;">removeChild</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h5>点击下面“运行”直接查看效果</h5>
<div class="runcode">
<p><textarea name="runcode" class="runcode_text" id="runcode_QUvdbd">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;前端思考&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;前端思考&quot; /&gt;
&lt;title&gt;弹出层—到浏览器中央：前端思考&lt;/title&gt;
&lt;style&gt;
*{margin:0;padding:0px;}
body{padding:30px;}
#show{width:300px;height:200px;display:none;padding:1px;position:absolute;border:1px solid #4c77aa;background:#f2f7fd;zoom:1;}
#show h3{background:#4c77aa;color:#fff;font-size:14px;padding:5px;}
#show span{position:absolute;right:3px;top:3px;display:block;cursor:pointer;color:#fff;font-weight:bold;}
#show p{padding:5px;}
#text{font-size:12px;text-indent:2em;line-height:20px;}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function show(){
        var iWidth = document.documentElement.clientWidth;
	var iHeight = document.documentElement.clientHeight;
	var oShow = document.getElementById('show');
	oShow.style.display = 'block';
        oShow.style.left = (iWidth-302)/2+&quot;px&quot;;
	oShow.style.top = (iHeight-202)/2+&quot;px&quot;;
	var oClose = document.createElement(&quot;span&quot;);
		oClose.innerHTML = &quot;×&quot;;
		oShow.appendChild(oClose);
		oClose.onclick = function(){
			oShow.style.display = 'none';
			oShow.removeChild(this);
		}
	}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a onclick=&quot;show(); return false&quot; href=&quot;#&quot;&gt;请猛击我（我会弹到中间）&lt;/a&gt;
&lt;div id=&quot;show&quot;&gt;
	&lt;h3&gt;弹出层标题栏&lt;/h3&gt;
	&lt;p id=&quot;text&quot;&gt;
		这里是弹出层内容，内容可以是文字、图片等，可以是iframe传进来，也可以用jQuery的load()传进来。如有什么不合理的或可以改进的，大家可以给我留言，共同学习。
	&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</textarea></p>
<p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_QUvdbd');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_QUvdbd');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<h4>三、弹出层—到浏览器中央—背景变暗</h4>
<h5>用到的javascript代码如下：</h5>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left2">Download <a href="http://www.artcss.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=477&amp;download=script.js">script.js</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4778"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p477code8"><pre class="javascript" style="font-family:Verdana;"><span style="color: #003366; font-weight: bold;">function</span> show<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> iWidth <span style="color: #339933;">=</span> document.<span style="color: #990000;">documentElement</span>.<span style="color: #990000;">clientWidth</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> iHeight <span style="color: #339933;">=</span> document.<span style="color: #990000;">documentElement</span>.<span style="color: #990000;">clientHeight</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> bgObj <span style="color: #339933;">=</span> document.<span style="color: #990000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #999999;">//创建背景层</span>
	bgObj.<span style="color: #990000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;bgbox&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	bgObj.<span style="color: #990000;">style</span>.<span style="color: #990000;">width</span> <span style="color: #339933;">=</span> iWidth<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
	bgObj.<span style="color: #990000;">style</span>.<span style="color: #990000;">height</span> <span style="color: #339933;">=</span>Math.<span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span>document.<span style="color: #990000;">body</span>.<span style="color: #990000;">clientHeight</span><span style="color: #339933;">,</span> iHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
	document.<span style="color: #990000;">body</span>.<span style="color: #990000;">appendChild</span><span style="color: #009900;">&#40;</span>bgObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		<span style="color: #999999;">//将创建的层插入body中</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> oShow <span style="color: #339933;">=</span> document.<span style="color: #990000;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'show'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
		oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">left</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>iWidth<span style="color: #339933;">-</span><span style="color: #ff0000;">302</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #ff0000;">2</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
		oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">top</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>iHeight<span style="color: #339933;">-</span><span style="color: #ff0000;">202</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #ff0000;">2</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> oClosebtn <span style="color: #339933;">=</span> document.<span style="color: #990000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oClosebtn.<span style="color: #990000;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;×&quot;</span><span style="color: #339933;">;</span>
		oShow.<span style="color: #990000;">appendChild</span><span style="color: #009900;">&#40;</span>oClosebtn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> oClose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
		oShow.<span style="color: #990000;">removeChild</span><span style="color: #009900;">&#40;</span>oClosebtn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		document.<span style="color: #990000;">body</span>.<span style="color: #990000;">removeChild</span><span style="color: #009900;">&#40;</span>bgObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
	oClosebtn.<span style="color: #990000;">onclick</span> <span style="color: #339933;">=</span> oClose<span style="color: #339933;">;</span>
	bgObj.<span style="color: #990000;">onclick</span> <span style="color: #339933;">=</span> oClose<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h5>点击下面“运行”直接查看效果</h5>
<div class="runcode">
<p><textarea name="runcode" class="runcode_text" id="runcode_wJj1Qp">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;前端思考&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;前端思考&quot; /&gt;
&lt;title&gt;弹出层—到浏览器中央—背景变暗：前端思考&lt;/title&gt;
&lt;style&gt;
*{margin:0;padding:0px;}
body{padding:30px;}
#show{width:300px;height:200px;display:none;padding:1px;position:absolute;border:1px solid #4c77aa;background:#f2f7fd;z-index:11;zoom:1;}
#show h3{background:#4c77aa;color:#fff;font-size:14px;padding:5px;}
#show span{position:absolute;right:3px;top:3px;display:block;cursor:pointer;color:#fff;font-weight:bold;}
#show p{padding:5px;}
#text{font-size:12px;text-indent:2em;line-height:20px;}
#bgbox{position:absolute;left:0;top:0;background:#000;filter:alpha(opacity=30);opacity:0.3;z-index:10}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function show(){
    var iWidth = document.documentElement.clientWidth;
	var iHeight = document.documentElement.clientHeight;
	var bgObj = document.createElement(&quot;div&quot;);
	bgObj.setAttribute(&quot;id&quot;,&quot;bgbox&quot;);
	bgObj.style.width = iWidth+&quot;px&quot;;
	bgObj.style.height =Math.max(document.body.clientHeight, iHeight)+&quot;px&quot;;
	document.body.appendChild(bgObj);
	var oShow = document.getElementById('show');
		  oShow.style.display = 'block';
          oShow.style.left = (iWidth-302)/2+&quot;px&quot;;
		  oShow.style.top = (iHeight-202)/2+&quot;px&quot;;
	function oClose(){
		oShow.style.display = 'none';
        document.body.removeChild(bgObj);
	}
	var oClosebtn = document.createElement(&quot;span&quot;);
		  oClosebtn.innerHTML = &quot;×&quot;;
		  oShow.appendChild(oClosebtn);
	oClosebtn.onclick = oClose;
	bgObj.onclick = oClose;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a onclick=&quot;show(); return false&quot; href=&quot;#&quot;&gt;请猛击我（我会弹到中间，同时背景变暗）&lt;/a&gt;
&lt;div id=&quot;show&quot;&gt;
	&lt;h3&gt;弹出层标题栏&lt;/h3&gt;
	&lt;p id=&quot;text&quot;&gt;
		这里是弹出层内容，内容可以是文字、图片等，可以是iframe传进来，也可以用jQuery的load()传进来。如有什么不合理的或可以改进的，大家可以给我留言，共同学习。
	&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</textarea></p>
<p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_wJj1Qp');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_wJj1Qp');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<h4>四、弹出层—到浏览器中央—背景变暗—支持键盘[Esc]退出</h4>
<h5>键盘事件的javascript代码如下：</h5>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left2">Download <a href="http://www.artcss.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=477&amp;download=script.js">script.js</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4779"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p477code9"><pre class="javascript" style="font-family:Verdana;"><span style="color: #003366; font-weight: bold;">function</span> getEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> window.<span style="color: #990000;">event</span> <span style="color: #339933;">||</span> arguments.<span style="color: #990000;">callee</span>.<span style="color: #990000;">caller</span>.<span style="color: #990000;">arguments</span><span style="color: #009900;">&#91;</span><span style="color: #ff0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #999999;">// 获得事件Event对象，用于兼容IE和FireFox</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
document.<span style="color: #990000;">onkeyup</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> event <span style="color: #339933;">=</span> getEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #990000;">keyCode</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">27</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		oClose<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h5>点击下面“运行”直接查看效果</h5>
<div class="runcode">
<p><textarea name="runcode" class="runcode_text" id="runcode_VCVYsD">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;前端思考&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;前端思考&quot; /&gt;
&lt;title&gt;弹出层—到浏览器中央—背景变暗—支持键盘[Esc]退出 ：前端思考&lt;/title&gt;
&lt;style&gt;
*{margin:0;padding:0px;}
body{padding:30px;}
#show{width:300px;height:200px;display:none;padding:1px;position:absolute;border:1px solid #4c77aa;background:#f2f7fd;z-index:11;zoom:1;}
#show h3{background:#4c77aa;color:#fff;font-size:14px;padding:5px;}
#show span{position:absolute;right:3px;top:3px;display:block;cursor:pointer;color:#fff;font-weight:bold;}
#show p{padding:5px;}
#text{font-size:12px;text-indent:2em;line-height:20px;}
#bgbox{position:absolute;left:0;top:0;background:#000;filter:alpha(opacity=30);opacity:0.3;z-index:10}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function show(){
	var iWidth = document.documentElement.clientWidth;
	var iHeight = document.documentElement.clientHeight;
	var bgObj = document.createElement(&quot;div&quot;);
	bgObj.setAttribute(&quot;id&quot;,&quot;bgbox&quot;);
	bgObj.style.width = iWidth+&quot;px&quot;;
	bgObj.style.height =Math.max(document.body.clientHeight, iHeight)+&quot;px&quot;;
	document.body.appendChild(bgObj);
	var oShow = document.getElementById('show');
		oShow.style.display = 'block';
		oShow.style.left = (iWidth-302)/2+&quot;px&quot;;
		oShow.style.top = (iHeight-202)/2+&quot;px&quot;;
	var oClosebtn = document.createElement(&quot;span&quot;);
		oClosebtn.innerHTML = &quot;×&quot;;
		oShow.appendChild(oClosebtn);
	function oClose(){
		oShow.style.display = 'none';
		oShow.removeChild(oClosebtn);
		document.body.removeChild(bgObj);
	}
	oClosebtn.onclick = oClose;
	bgObj.onclick = oClose;
	function getEvent() {
		return window.event || arguments.callee.caller.arguments[0];
	}
	document.onkeyup = function(){
		var event = getEvent();
		if (event.keyCode == 27){
			oClose();
		}
	}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a onclick=&quot;show(); return false&quot; href=&quot;#&quot;&gt;请猛击我（弹出层—到浏览器中央—背景变暗—支持键盘[Esc]退出）&lt;/a&gt;
&lt;div id=&quot;show&quot;&gt;
	&lt;h3&gt;弹出层标题栏&lt;/h3&gt;
	&lt;p id=&quot;text&quot;&gt;
		这里是弹出层内容，内容可以是文字、图片等，可以是iframe传进来，也可以用jQuery的load()传进来。如有什么不合理的或可以改进的，大家可以给我留言，共同学习。
	&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</textarea></p>
<p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_VCVYsD');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_VCVYsD');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<h4>四、弹出层—到浏览器中央—背景变暗—支持键盘[Esc]退出—支持鼠标拖动</h4>
<h5>鼠标拖动的javascript代码如下：</h5>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left2">Download <a href="http://www.artcss.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=477&amp;download=script.js">script.js</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p47710"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code" id="p477code10"><pre class="javascript" style="font-family:Verdana;"><span style="color: #003366; font-weight: bold;">var</span> moveX <span style="color: #339933;">=</span> <span style="color: #ff0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> moveY <span style="color: #339933;">=</span> <span style="color: #ff0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> moveTop <span style="color: #339933;">=</span> <span style="color: #ff0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> moveLeft <span style="color: #339933;">=</span> <span style="color: #ff0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> moveable <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> docMouseMoveEvent <span style="color: #339933;">=</span> document.<span style="color: #990000;">onmousemove</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> docMouseUpEvent <span style="color: #339933;">=</span> document.<span style="color: #990000;">onmouseup</span><span style="color: #339933;">;</span>
titleBar <span style="color: #339933;">=</span>  document.<span style="color: #990000;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'titlebar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
titleBar.<span style="color: #990000;">onmousedown</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> evt <span style="color: #339933;">=</span> getEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	moveable <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span> 
	moveX <span style="color: #339933;">=</span> evt.<span style="color: #990000;">clientX</span><span style="color: #339933;">;</span>
	moveY <span style="color: #339933;">=</span> evt.<span style="color: #990000;">clientY</span><span style="color: #339933;">;</span>
	moveTop <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">top</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	moveLeft <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">left</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	document.<span style="color: #990000;">onmousemove</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>moveable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> evt <span style="color: #339933;">=</span> getEvent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> moveLeft <span style="color: #339933;">+</span> evt.<span style="color: #990000;">clientX</span> <span style="color: #339933;">-</span> moveX<span style="color: #339933;">;</span>
			<span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> moveTop <span style="color: #339933;">+</span> evt.<span style="color: #990000;">clientY</span> <span style="color: #339933;">-</span> moveY<span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> x <span style="color: #339933;">&gt;</span> <span style="color: #ff0000;">0</span> <span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span> x <span style="color: #339933;">+</span> <span style="color: #ff0000;">302</span> <span style="color: #339933;">&lt;</span> iWidth<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> y <span style="color: #339933;">&gt;</span> <span style="color: #ff0000;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>y <span style="color: #339933;">+</span> <span style="color: #ff0000;">202</span> <span style="color: #339933;">&lt;</span> iHeight<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">left</span> <span style="color: #339933;">=</span> x <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
				oShow.<span style="color: #990000;">style</span>.<span style="color: #990000;">top</span> <span style="color: #339933;">=</span> y <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>	
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	document.<span style="color: #990000;">onmouseup</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>moveable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
			document.<span style="color: #990000;">onmousemove</span> <span style="color: #339933;">=</span> docMouseMoveEvent<span style="color: #339933;">;</span>
			document.<span style="color: #990000;">onmouseup</span> <span style="color: #339933;">=</span> docMouseUpEvent<span style="color: #339933;">;</span>
			moveable <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
			moveX <span style="color: #339933;">=</span> <span style="color: #ff0000;">0</span><span style="color: #339933;">;</span>
			moveY <span style="color: #339933;">=</span> <span style="color: #ff0000;">0</span><span style="color: #339933;">;</span>
			moveTop <span style="color: #339933;">=</span> <span style="color: #ff0000;">0</span><span style="color: #339933;">;</span>
			moveLeft <span style="color: #339933;">=</span> <span style="color: #ff0000;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> 
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h5>点击下面“运行”直接查看效果</h5>
<div class="runcode">
<p><textarea name="runcode" class="runcode_text" id="runcode_6jxSrT">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;前端思考&quot; /&gt;
&lt;meta name=&quot;keywords&quot; content=&quot;前端思考&quot; /&gt;
&lt;title&gt;五、弹出层—到浏览器中央—背景变暗—支持键盘[Esc]退出—支持鼠标拖动 ：前端思考&lt;/title&gt;
&lt;style&gt;
*{margin:0;padding:0px;}
body{padding:30px;}
#show{width:300px;height:200px;display:none;padding:1px;position:absolute;border:1px solid #4c77aa;background:#f2f7fd;z-index:11;zoom:1;}
#show h3{background:#4c77aa;color:#fff;font-size:14px;padding:5px;cursor:move}
#show span{position:absolute;right:3px;top:3px;display:block;cursor:pointer;color:#fff;font-weight:bold;}
#show p{padding:5px;}
#text{font-size:12px;text-indent:2em;line-height:20px;}
#bgbox{position:absolute;left:0;top:0;background:#000;filter:alpha(opacity=30);opacity:0.3;z-index:10}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function show(){
    var iWidth = document.documentElement.clientWidth;
	var iHeight = document.documentElement.clientHeight;
	var bgObj = document.createElement(&quot;div&quot;);
	bgObj.setAttribute(&quot;id&quot;,&quot;bgbox&quot;);
	bgObj.style.width = iWidth+&quot;px&quot;;
	bgObj.style.height =Math.max(document.body.clientHeight, iHeight)+&quot;px&quot;;
	document.body.appendChild(bgObj);
	var oShow = document.getElementById('show');
		oShow.style.display = 'block';
          oShow.style.left = (iWidth-302)/2+&quot;px&quot;;
		oShow.style.top = (iHeight-202)/2+&quot;px&quot;;
	var oClosebtn = document.createElement(&quot;span&quot;);
		oClosebtn.innerHTML = &quot;×&quot;;
		oShow.appendChild(oClosebtn);
	function oClose(){
		oShow.style.display = 'none';
        	document.body.removeChild(bgObj);
		oShow.removeChild(oClosebtn);
	}
	oClosebtn.onclick = oClose;
	bgObj.onclick = oClose;
        function getEvent() {
	    return window.event || arguments.callee.caller.arguments[0];
        }
	document.onkeyup = function(){
		var event = getEvent();
		if (event.keyCode == 27){
			oClose();
		}
	}
	var moveX = 0;
	var moveY = 0;
	var moveTop = 0;
	var moveLeft = 0;
	var moveable = false;
	var docMouseMoveEvent = document.onmousemove;
	var docMouseUpEvent = document.onmouseup;
	titleBar =  document.getElementById('titlebar');
	titleBar.onmousedown = function() {
		var evt = getEvent();
		moveable = true;
		moveX = evt.clientX;
		moveY = evt.clientY;
		moveTop = parseInt(oShow.style.top);
		moveLeft = parseInt(oShow.style.left);
		document.onmousemove = function() {
			if (moveable) {
				var evt = getEvent();
				var x = moveLeft + evt.clientX - moveX;
				var y = moveTop + evt.clientY - moveY;
				if ( x &gt; 0 &amp;&amp;( x + 302 &lt; iWidth) &amp;&amp; y &gt; 0 &amp;&amp; (y + 202 &lt; iHeight) ) {
					oShow.style.left = x + &quot;px&quot;;
					oShow.style.top = y + &quot;px&quot;;
				}
			}
		};
		document.onmouseup = function () {
			if (moveable) {
				document.onmousemove = docMouseMoveEvent;
				document.onmouseup = docMouseUpEvent;
				moveable = false;
				moveX = 0;
				moveY = 0;
				moveTop = 0;
				moveLeft = 0;
			}
		};
	}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a onclick=&quot;show(); return false&quot; href=&quot;#&quot;&gt;请猛击我（弹出层—到浏览器中央—背景变暗—支持键盘[Esc]退出—支持鼠标拖动）&lt;/a&gt;
&lt;div id=&quot;show&quot;&gt;
	&lt;h3 id=&quot;titlebar&quot;&gt;弹出层标题栏&lt;/h3&gt;
	&lt;p id=&quot;text&quot;&gt;
		这里是弹出层内容，内容可以是文字、图片等，可以是iframe传进来，也可以用jQuery的load()传进来。如有什么不合理的或可以改进的，大家可以给我留言，共同学习。
	&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</textarea></p>
<p><input type="button" value="运行" class="runcode_button" onclick="runcode_open_new('runcode_6jxSrT');"/> <input type="button" value="复制" class="runcode_button" onclick="runcode_copy('runcode_6jxSrT');"/> 提示：你可以先修改部分代码再运行。</p>
</div>
<p>欢迎大家多提改进意见或建议，共同学习和探讨</p>
]]></content:encoded>
			<wfw:commentRss>http://www.artcss.com/archives/477.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

