Home  >  Article  >  Web Front-end  >  Separation of style and behavior when implementing lightBox. Reduce JS_javascript techniques. Single line centering, multi-line centering on the left. Unknown height vertical centering method.

Separation of style and behavior when implementing lightBox. Reduce JS_javascript techniques. Single line centering, multi-line centering on the left. Unknown height vertical centering method.

PHP中文网
PHP中文网Original
2016-05-16 18:49:011064browse

This tutorial aims to separate the style and behavior of lightBox and reduce the work of JS in various aspects (full screen masking, masking select in ie6, two-way centering, height adaptive content, etc.).

First enter the 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.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="author" content="Chomo" /> 
<link rel="start" href="http://www.14px.com" title="Home" /> 
<title>全屏屏蔽、自动居中的lightBox</title> 
<style type="text/css"> 
* { margin:0; padding:0; } 
html,body { height:100%; overflow:hidden; font:12px/1.5 simsun;} 
.myPage { line-height:3; overflow:auto; width:100%; height:100%;} 
.lightBox, 
.popupCover, 
.popupIframe, 
.popupComponent { position:absolute; left:0; top:0; width:100%; height:100%;} 
.popupComponent { z-index:2; display:none;} 
.popupIframe { display:none; _display:block; _filter:alpha(opacity=0);} 
.popupCover { background:#000; opacity:0.7; *filter:alpha(opacity=70);} 
.lightBox { text-align:center; overflow:auto;} 
.lightBoxContent { display:inline-block; *display:inline; zoom:1; width:300px; padding:10px; background:#fff; border:5px solid #00b4ff; vertical-align:middle;} 
.lightBoxMaxHeight { display:inline-block; vertical-align:middle; height:100%; *height:99.5%; width:1px; overflow:hidden; margin-left:-1px;} 
.lightBoxWrapper { display:inline-block; *display:inline; zoom:1; text-align:left;} 
.lightBoxClose { color:#f00;} 
.lightBoxSubmit { margin-top:10px; padding-top:5px; border-top:1px solid #ccc;} 
.lightBoxSubmit input { font-size:12px; padding:0 10px; overflow:visible; margin:0 5px;} 
</style> 
</head> 
<body> 
<p class="popupComponent" id="lightBox"> 
<iframe class="popupIframe"></iframe> 
<p class="popupCover"></p> 
<p class="lightBox"> 
<span class="lightBoxMaxHeight"></span> 
<p class="lightBoxContent"> 
<p class="lightBoxWrapper"> 
当提示小于一行时文字居中[br] 
</p> 
<p class="lightBoxSubmit"> 
<input type="button" value="确定" onclick="hideLayer(&#39;lightBox&#39;)" /> 
<input type="button" value="取消" onclick="hideLayer(&#39;lightBox&#39;)" /> 
</p> 
</p> 
</p> 
</p> 
<p class="popupComponent" id="lightBox2"> 
<iframe class="popupIframe"></iframe> 
<p class="popupCover"></p> 
<p class="lightBox"> 
<p class="lightBoxContent"> 
<p class="lightBoxWrapper"> 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度 当内容已经超过了一屏的高度[br] 
</p> 
<p class="lightBoxSubmit"> 
<input type="button" value="确定" onclick="hideLayer(&#39;lightBox2&#39;)" /> 
<input type="button" value="取消" onclick="hideLayer(&#39;lightBox2&#39;)" /> 
</p> 
</p> 
<span class="lightBoxMaxHeight"></span> 
</p> 
</p> 
<p class="myPage"> 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
<p style="text-align:center;"><input type="button" value="位于第一屏的按钮" onclick="showLayer(&#39;lightBox&#39;)" /></p> 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
<select><option>一个用于测试IE6中是否能覆盖住的select</option></select>[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
<select><option>又一个用于测试IE6中是否能覆盖住的select</option></select>[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
<p style="text-align:center;"><input type="button" value="位于第二屏中的按钮" onclick="showLayer(&#39;lightBox&#39;)" /><input type="button" value="当内容已经超过了一屏的高度" onclick="showLayer(&#39;lightBox2&#39;)" /></p> 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
</p> 
<script> 
function showLayer(id) { 
document.getElementById(id).style.display = "block"; 
} 
function hideLayer(id) { 
document.getElementById(id).style.display = "none"; 
} 
</script> 
</body> 
</html>

This lightBox has the following features, you can test it by modifying the code^-^:
Click anywhere on the page When it is turned on, the screen can be perfectly shielded (any element including the scroll bar cannot be directly controlled at this time). In IE6, the select element can be shielded without hiding it (the flaw of the iframe shielding method: after shielding, if the scroll bar is dragged, the select element remains will be moved to the top).
The height of the lightBox adapts to its content and appears perfectly centered.
When the content is a single line, it is displayed in the center; when the content is multiple lines, it is displayed on the left; when the height of the content is such that the lightBox exceeds the screen height, a scroll bar is displayed and displayed at the top, so that readers can read it completely.
If you are interested, the following tips to construct this lightBox are available for your reference:

1. Full screen masking method:

A. Hide the scroll bar of html/body.
B. Use a p as a mask, and then use a p to make a page with scroll bars, one on top and one on the bottom.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="author" content="Chomo" /> 
<link rel="start" href="http://www.14px.com" title="Home" /> 
<title>全屏遮蔽的方法</title> 
<style type="text/css"> 
* { 
margin:0; padding:0; /*--- 简单的css reset,据说它的效率不高,请自行替换为高效的 ---*/ 
} 
html,body { 
height:100%; /*--- 有了这个高度,IE6下,body的子对象设置height:100%才有效 ---*/ 
overflow:hidden; /*--- 灭掉html或body产生的滚动条 ---*/ 
} 
.page { 
line-height:3; 
overflow:auto; /*--- 滚动条就靠它了 ---*/ 
width:600px; height:400px; background:#f60; /*--- 暂时先做这么大,方便大家看清楚 ---*/ 
position:relative; left:50px; top:50px; /*--- 移动一点点,方便大家看清楚,把它去掉,遮罩的基本的效果就实现了 ---*/ 
} 
.cover { 
position:absolute; 
width:600px; height:400px; background:#000; 
opacity:0.7; *filter:alpha(opacity=70); 
z-index:2; 
} 
</style> 
</head> 
<body> 
<p class="cover"></p> 
<p class="page"> 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br]假装很丰富的内容[br] 
</p> 
</body> 
</html>

2. The method of centering a single line and centering multiple lines on the left (see the demo, the expression ability is limited):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="author" content="Chomo" /> 
<link rel="start" href="http://www.14px.com" title="Home" /> 
<title>单行居中,多行居左</title> 
<style type="text/css"> 
* { 
margin:0; padding:0; 
} 
.outer { margin-top:20px; text-align:center;} 
.inner { display:inline-block; *display:inline; zoom:1; text-align:left;} 
</style> 
</head> 
<body> 
<div class="outer"> 
<div class="inner">内容很少时</div> 
</div> 
<div class="outer"> 
<div class="inner"> 
<p>内容成段时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时时</p> 
</div> 
</div> 
</body> 
</html>

3. Use The method of inline-block to achieve vertical centering of unknown height (see the picture and demo, the expression ability is limited):

Illustration:

Code Demo:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<meta name="author" content="Chomo" /> 
<link rel="start" href="http://www.14px.com" title="Home" /> 
<title>未知高度垂直居中的方法</title> 
<style type="text/css"> 
* { margin:0; padding:0;} 
html,body { height:100%; overflow:hidden; text-align:center; font-size:0; } 
.maxHeight { display:inline-block; *display:inline; *zoom:1; height:100%; width:10px; vertical-align:middle; background:#00b4ff; margin-left:-10px;} 
.lightBox { display:inline-block; *display:inline; *zoom:1; width:300px; border:2px solid #f60; font-size:12px; background:#fff; position:relative; vertical-align:middle;} 
</style> 
</head> 
<body> 
<div class="lightBox"> 
<br><br><br><br><br> 
</div> 
<div class="maxHeight"></div> 
</body> 
</html>

There are two detailed problems:
You can see that font-size:0; does not appear in the complete demo, but in the vertical centering method appeared in the middle.
Because font-size:0 in the complete demo will make the mouse wheel of the lightBox invalid when readers in Firefox 3.5 read it completely. The reason is unknown. You can deal with this as you please.
maxHeight can only be placed behind lightBoxContent, because there will be a bug in opera9.6, embarrassing.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn