html:
<button onclick=""bombClick()" >点击弹框</button>
<div id="bombContain"></div>
<div id="bomb_div"></div>
css:
#bombContain{
z-index: 199412204822//z-index只有在定位元素上生效,默认值static是没有定位的
background-color:#000
opacity:0.6
top:0
left:0
width:100%
height:100%
position:fixed
display:none
}
#bomb_div{
z-index:199412204822
width:34%
height:450px
top:66px
left:36%
margin:0
padding:0
background-color:#fff
box-shadow:1px 1px 50px rgba(0,0,0,.3)
position:fixed
display:none
}
js:
function bombClick(){
$("#bombContain").css("display","block")
$("#bomb_div").css("display","block")
}
CSS部分
div{width: 400px
height: 400px
border:1px solid red
float: left
}
body:hover .box{
display: block
}
.box{
width: 200px
height: 200px
background: red
display: none
}
html部分
<div ><div class="box">
</div>
</div>
<script type="text/javascript">function init() {
// 获取文档尺寸
var documentSize = getDocumentSize()
// 获取视窗尺寸
var windowSize = getWindowSize()
// 对比大小,取大值
var pageWidth = documentSize.x >windowSize.x ? documentSize.x : windowSize.x
var pageHeight = documentSize.y >windowSize.y ? documentSize.y : windowSize.y
// 底层DIV(用于遮挡其他元素)
var mask = document.createElement("div")
mask.id = "login_mask"
setStyle(mask, 'left', 0)
setStyle(mask, 'top', 0)
setStyle(mask, 'background', '#000000')
setStyle(mask, 'width', pageWidth+'px')
setStyle(mask, 'height', pageHeight+'px')
setStyle(mask, 'position', 'absolute')
setStyle(mask, 'filter', 'Alpha(opacity=40)')
setStyle(mask, '-moz-opacity', '0.4')
setStyle(mask, 'opacity', '0.4')
setStyle(mask, 'zIndex', 100)
// 底层ifreme(用于挡住select下拉框等普通DIV无法遮挡的元素)
var iframe = document.createElement("iframe")
iframe.id = "login_iframe"
setStyle(iframe, 'left', 0)
setStyle(iframe, 'top', 0)
setStyle(iframe, 'background', '#000000')
setStyle(iframe, 'width', pageWidth+'px')
setStyle(iframe, 'height', pageHeight+'px')
setStyle(iframe, 'position', 'absolute')
setStyle(iframe, 'filter', 'Alpha(opacity=0)')
setStyle(iframe, '-moz-opacity', '0')
setStyle(iframe, 'opacity', '0')
setStyle(iframe, 'zIndex', 99)
document.body.appendChild(iframe)
document.body.appendChild(mask)
// 弹出DIV
var popDiv = document.createElement("div")
var popTop = 100
var popLeft = 100
setStyle(popDiv, 'left', '100px')
setStyle(popDiv, 'top', '200px')
setStyle(popDiv, 'width', '300px')
setStyle(popDiv, 'height', '200px')
setStyle(popDiv, 'background', '#FFF')
setStyle(popDiv, 'zIndex', 1000)
setStyle(popDiv, 'display', 'block')
setStyle(popDiv, 'position', 'absolute')
document.body.appendChild(popDiv)
}
function getWindowSize() {
var client = {
x:0,
y:0
}
if(typeof document.compatMode != 'undefined' &&document.compatMode == 'CSS1Compat') {
client.x = document.documentElement.clientWidth
client.y = document.documentElement.clientHeight
} else if (typeof document.body != 'undefined') {
client.x = document.body.clientWidth
client.y = document.body.clientHeight
}
return client
}
function getDocumentSize() {
var doc = {
x:0,
y:0
}
doc.x = document.body.scrollWidth
doc.y = document.body.scrollHeight
return doc
}
function setStyle(element, key, value) {
element.style[key] = value
}
</script>
<body onload="init()">