html页面弹出窗体
在现在的网页上面经常可以看到弹出窗体,以前自己觉得在浏览器上实现这个肯定是非常繁琐的,学了些css后自己也尝试着做了做,写完了之后其实感觉也挺简单的。
首先想要实现弹出一个窗口需要事先写两个<div>层:
<div id="content">
这是一个弹出的窗口<button onclick="closeIt()" style="margin-left:80%">关闭</button>
<br />
<input type="password" />
</div>
<div id="overlay">
</div>
overlay层是用来覆盖整个页面的,也就是做到锁定当前页面的功能,content层则可以放弹出的元素。
他们的css样式:
<style>
#overlay{
display:none;
position:absolute;
top:0%;
left:0%;
height:100%;
width:100%;
background-color:#000000;
z-index:1001;
opacity:.5;
}
#content{
border:8px solid #CCCCCC;
position:absolute;
width:50%;
height:50%;
left:25%;
top:25%;
background-color:#FFFFFF;
z-index:1002;
overflow:auto;
display:none;
}
</style>
通过 z-index属相的设置,使得content层在overlay层的上面,
这里介绍一下opacity属性,这个属性可以很容易的实现透明效果,而且现在的浏览器都支持了,起码连新版的IE都支持了。
<!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=gb2312" />
<title>无标题文档</title>
<style>
#overlay{
display:none;
position:absolute;
top:0%;
left:0%;
height:100%;
width:100%;
background-color:#000000;
z-index:1001;
opacity:.5;
}
#content{
border:8px solid #CCCCCC;
position:absolute;
width:50%;
height:50%;
left:25%;
top:25%;
background-color:#FFFFFF;
z-index:1002;
overflow:auto;
display:none;
}
</style>
<script type="text/javascript">
function doIt(){
document.getElementById("content").style.display='block';
document.getElementById("overlay").style.display='block';
}
function closeIt(){
document.getElementById("content").style.display='none';
document.getElementById("overlay").style.display='none';
}
</script>
</head>
<body>
<br />
<button onclick="doIt()">弹出来</button>
<div id="content">
这是一个弹出的窗口<button onclick="closeIt()" style="margin-left:80%">关闭</button>
<br />
<input type="password" />
</div>
<div id="overlay">
</div>
</body>
</html>
浙公网安备 33010602011771号