`

center popup div

阅读更多
popup center div
<html>
	<head>
		<title>div 100% height</title>
		<style>
			html, body {
				height: 100%;
				width: 100%;
				margin: 0;
				padding: 0;
			}
			#t {
				position: absolute;
				left: 5%;
				top: 5%;
				border: 1px solid #777;
				height: 90%;
				width: 90%;
			}
		</style>
		<script>
			var isIE;
			function showList(txt){
				//for non-IE browser
				var fixX=0;
				var fixY=0;
				if(isIE){
					fixX=0;
					fixY=0;
				}
				var e=document.getElementById("divList");
				var x=txt.offsetLeft;
				var y=txt.offsetTop;
				var w=txt.offsetWidth;
				var h=txt.offsetHeight;
				e.style.display="block";
				//it's adjust prefect for chrome, it should adjust again if for FireFox or IE
				e.style.left=2+"px";
				e.style.top=-2+"px";
				e.style.width=w-2+"px";
				e.style.height=100+"px";
			}
			function popupDiv(e){
				e.style.display="block";
				
				e.style.left="50%";
				e.style.top="50%";
				e.style.marginLeft=-e.offsetWidth/2+"px";
				e.style.marginTop=-e.offsetHeight/2+"px";
			}
			function selectItem(e){
				var txt=e.innerHTML;
				var e=document.getElementById("txt");
				e.value=txt;
				document.getElementById("divList").style.display="none";
			}
			window.onload=function(){
				document.getElementById('lblBrowser').innerHTML=navigator.appName+", "+navigator.appVersion;
				var str=navigator.appName;
				var reg=/internet\ explorer/i;
				if(reg.test(str))
					isIE=true;
			}
		</script>
	</head>
	<body>
		<div id="t">	
			100% height div<br />
			browser: <label id="lblBrowser"></label><br />
			<input type="button" value="popup center div" onclick="popupDiv(document.getElementById('divCenter'))" />
		</div>
		<div id="divCenter" style="position: absolute; display: none; width: 300px; height: 200px; border: 1px solid blanchedalmond;">
			<!-- <div>		for the bug about IE marginLeft -50% --->
				Hi, where are you travel this year?<br />
				<input type="text" id="txt" name="txt" onclick="showList(this)" /><br />
				<div id="divList" style="display: none; border: 1px solid #777; position: relative;">
					<ul>
						<li onclick="selectItem(this)">SINGAPORE</li>
						<li onclick="selectItem(this)">MALAYSIA</li>
						<li onclick="selectItem(this)">INDONISIA</li>
					</ul>
				</div>
				<input type="button" value="close" onclick="document.getElementById('divCenter').style.display='none';" />
			<!--</div>--->
		</div>
	</body>
</html>


it's respond incorrect in IE, the input text will go away into left, I believe it's the bug of IE, cause by margin left,
so the solution is add a div as container for it. try to remove the annotation. now it compatible in IE, FF, Chrome.
final code:

<html>
	<head>
		<title>div 100% height</title>
		<style>
			html, body {
				height: 100%;
				width: 100%;
				margin: 0;
				padding: 0;
			}
			#t {
				position: absolute;
				left: 5%;
				top: 5%;
				border: 1px solid #777;
				height: 90%;
				width: 90%;
			}
		</style>
		<script>
			var isIE;
			function showList(txt){
				//for non-IE browser
				var fixX=0;
				var fixY=0;
				var fixW=0;
				if(isIE){
					fixX=-2;
					fixY=0;
					fixW=2;
				}
				var e=document.getElementById("divList");
				var x=txt.offsetLeft;
				var y=txt.offsetTop;
				var w=txt.offsetWidth;
				var h=txt.offsetHeight;
				e.style.display="block";
				//it's adjust prefect for chrome, it should adjust again if for FireFox or IE
				e.style.left=2+fixX+"px";
				e.style.top=-2+fixY+"px";
				e.style.width=w-2+fixW+"px";
				e.style.height=100+"px";
			}
			function popupDiv(e){
				e.style.display="block";
				
				e.style.left="50%";
				e.style.top="50%";
				e.style.marginLeft=-e.offsetWidth/2+"px";
				e.style.marginTop=-e.offsetHeight/2+"px";
			}
			function selectItem(e){
				var txt=e.innerHTML;
				var e=document.getElementById("txt");
				e.value=txt;
				document.getElementById("divList").style.display="none";
			}
			window.onload=function(){
				document.getElementById('lblBrowser').innerHTML=navigator.appName+", "+navigator.appVersion;
				var str=navigator.appName;
				var reg=/internet\ explorer/i;
				if(reg.test(str))
					isIE=true;
			}
		</script>
	</head>
	<body>
		<div id="t">	
			100% height div<br />
			browser: <label id="lblBrowser"></label><br />
			<input type="button" value="popup center div" onclick="popupDiv(document.getElementById('divCenter'))" />
		</div>
		<div id="divCenter" style="position: absolute; display: none; width: 300px; height: 200px; border: 1px solid blanchedalmond;">
			<div>
				Hi, where are you travel this year?<br />
				<input type="text" id="txt" name="txt" onclick="showList(this)" /><br />
				<div id="divList" style="display: none; border: 1px solid #777; position: relative;">
					<ul>
						<li onclick="selectItem(this)">SINGAPORE</li>
						<li onclick="selectItem(this)">MALAYSIA</li>
						<li onclick="selectItem(this)">INDONISIA</li>
					</ul>
				</div>
				<input type="button" value="close" onclick="document.getElementById('divCenter').style.display='none';" />
			</div>
		</div>
	</body>
</html>


the result as below:

chrome1



chrome2



IE1



IE2



  • 大小: 58.7 KB
  • 大小: 67.2 KB
  • 大小: 89.9 KB
  • 大小: 69.5 KB
分享到:
评论

相关推荐

    无限菜单之 xml+popup 版(IE5.5+)

    因为这些特征,Popup窗口制作的菜单比起传统的div(层)实现的菜单有着得天独厚的优势,不仅效果会非常好,而且代码也会是非常少的,只是对于实现起来却有几个需要解决的棘手问题:多个Popup共存的问题、如何递归生成...

    轮播服装饰品鞋帽美容护肤皮具包 免费淘宝装修模板

    促销模板代码,安装简单易懂 &lt;DIV class="slider-promo J_Slider J_TWidget" style="FLOAT: left; OVERFLOW: hidden;...&lt;DIV align=center&gt;&lt;A href="http://favorite.taobao.com/popup/add_collection....

    用DIV完美模拟createPopup 弹出窗口(脚本之家修正版),支持Firefox,ie,chrome

    模拟Popup body{ margin:0; padding:0; text-align:center; } #container{ margin:50px auto; padding:10px auto; } #popupcontent{ position:absolute; border:1px solid #000000; line-height:17px; ...

    客服模块左侧代码

    " href="http://favorite.taobao.com/popup/add_collection_2.htm?id=65974808&amp;itemtype=0&amp;is_tmall=&amp;is_lp=&amp;is_taohua" target="_blank"&gt;...

    Dreamweaver CS4 黄金插件10-02

    20. Center Popup Window V1.0.1 For Adobe Dreamweaver 21. Check Form V4.66.0 For Adobe Dreamweaver 22. Clear Initial Form Fields V2.1.1 For Adobe Dreamweaver 23. Close All Documents V1.0.5 For Adobe ...

    Dreamweaver CS4 黄金插件10-05

    20. Center Popup Window V1.0.1 For Adobe Dreamweaver 21. Check Form V4.66.0 For Adobe Dreamweaver 22. Clear Initial Form Fields V2.1.1 For Adobe Dreamweaver 23. Close All Documents V1.0.5 For Adobe ...

    Dreamweaver CS4 黄金插件10-1

    20. Center Popup Window V1.0.1 For Adobe Dreamweaver 21. Check Form V4.66.0 For Adobe Dreamweaver 22. Clear Initial Form Fields V2.1.1 For Adobe Dreamweaver 23. Close All Documents V1.0.5 For Adobe ...

    Dreamweaver CS4 黄金插件10-03

    20. Center Popup Window V1.0.1 For Adobe Dreamweaver 21. Check Form V4.66.0 For Adobe Dreamweaver 22. Clear Initial Form Fields V2.1.1 For Adobe Dreamweaver 23. Close All Documents V1.0.5 For Adobe ...

    Dreamweaver CS4 黄金插件10-04

    20. Center Popup Window V1.0.1 For Adobe Dreamweaver 21. Check Form V4.66.0 For Adobe Dreamweaver 22. Clear Initial Form Fields V2.1.1 For Adobe Dreamweaver 23. Close All Documents V1.0.5 For Adobe ...

    Dreamweaver CS4 黄金插件10-08

    20. Center Popup Window V1.0.1 For Adobe Dreamweaver 21. Check Form V4.66.0 For Adobe Dreamweaver 22. Clear Initial Form Fields V2.1.1 For Adobe Dreamweaver 23. Close All Documents V1.0.5 For Adobe ...

    Dreamweaver CS4 黄金插件10-06

    20. Center Popup Window V1.0.1 For Adobe Dreamweaver 21. Check Form V4.66.0 For Adobe Dreamweaver 22. Clear Initial Form Fields V2.1.1 For Adobe Dreamweaver 23. Close All Documents V1.0.5 For Adobe ...

    Dreamweaver CS4 黄金插件10-07

    20. Center Popup Window V1.0.1 For Adobe Dreamweaver 21. Check Form V4.66.0 For Adobe Dreamweaver 22. Clear Initial Form Fields V2.1.1 For Adobe Dreamweaver 23. Close All Documents V1.0.5 For Adobe ...

    ExtAspNet_v2.3.2_dll

    -使用Hidden控制Window控件的显示隐藏,Popup已经标记为Obsolete属性。 -Window的实例方法GetCloseReference等以及ActiveWindow的静态方法GetCloseReference等,其中的Close全部改为Hide。 -增加TabStrip中Tab...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -使用Hidden控制Window控件的显示隐藏,Popup已经标记为Obsolete属性。 -Window的实例方法GetCloseReference等以及ActiveWindow的静态方法GetCloseReference等,其中的Close全部改为Hide。 -增加TabStrip中Tab...

Global site tag (gtag.js) - Google Analytics