其实弹出层的思路还是比较简单的:
一般是这个层是隐藏的(display:none),然后点击你说的"图片(或者任何的页面元素)",这个层就会显示(一般display:inline-block/block),同时设置一个灰色的背景看起来这个层是浮于下面的层之上的。
下面是一段演示代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8"><title>弹出层—到浏览器中央—背景变暗</title>
<style>
*{margin:0padding:0px}
body{padding:30px}
#show{width:300pxheight:200pxdisplay:nonepadding:1pxposition:absoluteborder:1px solid #4c77aabackground:#f2f7fdz-index:11zoom:1}
#show h3{background:#4c77aacolor:#ffffont-size:14pxpadding:5px}
#show span{position:absoluteright:3pxtop:3pxdisplay:blockcursor:pointercolor:#ffffont-weight:bold}
#show p{padding:5px}
#text{font-size:12pxtext-indent:2emline-height:20px}
#bgbox{position:absoluteleft:0top:0background:#000filter:alpha(opacity=30)opacity:0.3z-index:10}
</style>
<script type="text/javascript">
function show(){
var iWidth = document.documentElement.clientWidth
var iHeight = document.documentElement.clientHeight
var bgObj = document.createElement("div")
bgObj.setAttribute("id","bgbox")
bgObj.style.width = iWidth+"px"
bgObj.style.height =Math.max(document.body.clientHeight, iHeight)+"px"
document.body.appendChild(bgObj)
var oShow = document.getElementById('show')
oShow.style.display = 'block'
oShow.style.left = (iWidth-302)/2+"px"
oShow.style.top = (iHeight-202)/2+"px"
function oClose(){
oShow.style.display = 'none'
document.body.removeChild(bgObj)
}
var oClosebtn = document.createElement("span")
oClosebtn.innerHTML = "×"
oShow.appendChild(oClosebtn)
oClosebtn.onclick = oClose
bgObj.onclick = oClose
}
</script>
</head>
<body>
<a onclick="show() return false" href="#">请猛击我(我会弹到中间,同时背景变暗)</a>
<div id="show"><h3>弹出层标题栏</h3>
<p id="text">
这里是弹出层内容,内容可以是文字、图片等,可以是iframe传进来,也可以用jQuery的load()传进来。
</p>
</div>
</body>
</html>
<script type="text/javascript">
function showDiv(){
document.getElementById('popDiv').style.display='block'
document.getElementById('bg').style.display='block'
}
function closeDiv(){
document.getElementById('popDiv').style.display='none'
document.getElementById('bg').style.display='none'
}
</script>
<div id="popDiv" style="z-index:99display:noneposition:absolutemargin-top: 20%margin-left: 40%background-color: #FFF">
<h3>这里的html自己写,样式什么的自定义</h3>
<form action="#">
<p><span>账号:</span><input type="text"></p>
<p><span>密码:</span><input type="password"></p>
</form>
<br/>
<a href="javascript:closeDiv()">关闭遮罩层</a></div>
<div id="bg" style="display:nonebackground-color: #cccwidth: 100%position:absoluteheight: 100%opacity: 0.5z-index: 1"></div>
<div style="padding-top: 10%padding-left:40%z-index:1">
<input type="Submit" name="" value="打开遮罩层" onclick="javascript:showDiv()" />
</div>
效果图: