代码如下:
//--------------------弹出层-------------------
//popDivId:弹出层div的ID
//dragDivId:用于拖动div的ID
//isShowMask:是否显示遮罩层
function
popDivShow(popDivId,
dragDivId,
isShowMask)
{
if
(isShowMask)
{
creatMask(popDivId)
}
var
oWins
=
document.getElementById(popDivId)
var
oWins_title
=
document.getElementById(dragDivId)
var
bDrag
=
false
var
disX
=
disY
=
0
oWins.style.display
=
"block"
oWins_title.onmousedown
=
function(event)
{
var
event
=
event
||
window.event
bDrag
=
true
disX
=
event.clientX
-
oWins.offsetLeft
disY
=
event.clientY
-
oWins.offsetTop
this.setCapture
&&
this.setCapture()
return
false
}
document.onmousemove
=
function(event)
{
if
(!bDrag)
return
var
event
=
event
||
window.event
var
iL
=
event.clientX
-
disX
var
iT
=
event.clientY
-
disY
var
maxL
=
document.documentElement.clientWidth
-
oWins.offsetWidth
var
maxT
=
document.documentElement.clientHeight
-
oWins.offsetHeight
iL
=
iL
<
0
?
0
:
iL
iL
=
iL
>
maxL
?
maxL
:
iL
iT
=
iT
<
0
?
0
:
iT
iT
=
iT
>
maxT
?
maxT
:
iT
oWins.style.marginTop
=
oWins.style.marginLeft
=
0
oWins.style.left
=
iL
+
"px"
oWins.style.top
=
iT
+
"px"
return
false
}
document.onmouseup
=
window.onblur
=
oWins_title.onlosecapture
=
function()
{
bDrag
=
false
oWins_title.releaseCapture
&&
oWins_title.releaseCapture()
}
}
//
隐藏弹出层
function
popDivHidden(popDivId)
{
var
oWins
=
document.getElementById(popDivId)
oWins.style.display
=
"none"
window.parent.document.body.removeChild(window.parent.document.getElementById("maskDiv"))
}
//
获取弹出层的zIndex
function
getZindex(popDivId)
{
var
popDiv
=
document.getElementById(popDivId)
var
popDivZindex
=
popDiv.style.zIndex
return
popDivZindex
}
//
创建遮罩层
function
creatMask(popDivId)
{
//
参数w为弹出页面的宽度,参数h为弹出页面的高度,参数s为弹出页面的路径
var
maskDiv
=
window.parent.document.createElement("div")
maskDiv.id
=
"maskDiv"
maskDiv.style.position
=
"fixed"
maskDiv.style.top
=
"0"
maskDiv.style.left
=
"0"
maskDiv.style.zIndex
=
getZindex(popDivId)
-
1
maskDiv.style.backgroundColor
=
"#333"
maskDiv.style.filter
=
"alpha(opacity=70)"
maskDiv.style.opacity
=
"0.7"
maskDiv.style.width
=
"100%"
maskDiv.style.height
=
(window.parent.document.body.scrollHeight
+
50)
+
"px"
window.parent.document.body.appendChild(maskDiv)
maskDiv.onmousedown
=
function()
{
window.parent.document.body.removeChild(window.parent.document.getElementById("maskDiv"))
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>定时弹窗</title>
<style type="text/css">
html,body{
width:100%
height:100%
margin:0
padding:0
}
.box{
width:100%
}
.tc-box{
width:25%
margin:80px auto
border:1px solid #f00
text-align: center
line-height: 150px
}
</style>
</head>
<body>
<div class="box">
<div class="tc-box" id="tc">
<div class="tc-cont">我是弹窗内容</div>
<button id="btn">点击关闭</button>
</div>
</div>
<script type="text/javascript">
var btn = document.getElementById('btn')
btn.onclick = function(){
var tc = document.getElementById('tc')
tc.style.display = 'none'
}
function xs(){
var tc = document.getElementById('tc')
tc.style.display = 'block'
console.log('aaaaaaa')
}
var lia = window.setInterval(xs,5000)
</script>
</body>
</html>
点击关闭之后5秒就会自动弹出
上面的代码有点不太合理,下面代码优化了一下。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>定时弹窗</title>
<style type="text/css">
html,body{
width:100%
height:100%
margin:0
padding:0
}
.box{
width:100%
}
.tc-box{
width:35%
margin:80px auto
border:1px solid #f00
text-align: center
line-height: 150px
}
</style>
</head>
<body>
<div class="box">
<div class="tc-box" id="tc">
<div class="tc-cont">我是弹窗内容</div>
<button id="btn">点击关闭5秒后继续弹出</button>
<button id="btn1">点击关闭,不再弹窗</button>
</div>
</div>
<script type="text/javascript">
var btn = document.getElementById('btn')
function xs(){
var tc = document.getElementById('tc')
tc.style.display = 'block'
tc.classList.remove('on')
console.log('aaaaaaa')
}
btn.onclick = function(){
var tc = document.getElementById('tc')
tc.style.display = 'none'
var lia = window.setInterval('xs()',5000)
tc.classList.add('on')//添加一个on类
if(tc.classList.contains('on') == true){ //查询类
function dsq(){
window.clearInterval(lia)
}
window.setTimeout(dsq,5000)
}
}
// 清除定时器
var btn1 = document.getElementById('btn1')
btn1.onclick = function(){
var tc = document.getElementById('tc')
tc.style.display = 'none'
}
</script>
</body>
</html>
因为一直弹窗会对浏览器产生很大的负荷,这里除了添加不再弹窗之外还优化了一下代码。
之前的代码是点击之后开始执行循环函数,但是不停止循环,不管弹窗是否显示的状态都会每5秒执行一次,这样明显不合理,应该是在你关闭弹窗之后5秒才执行。弹窗在显示状态不执行。