<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.box {
width: 200px;
height: 200px;
position: fixed;
color: #f4f5f6;
right: 0;
bottom: 0;
background-color: #21528080;
display: none;
background: url(../images/niu3.gif) no-repeat center center;
}
b {
float: right;
line-height: 20px;
background-color: #333;
color: #fff;
}
</style>
</head>
<body>
<div class="box" id="ad">
<p><span id="s">10</span>s后自动关闭<b id="b">×</b></p>
</div>
<script>
var oad = $('ad');
var os = $('s');
var ob = $('b');
var t;
fn();
function fn() {
//设置当网页打开1秒的时候弹出广告
setTimeout(function () {
os.innerHTML = 10;//让每一次重新出现广告是从10秒开始
//打开网页的时候将广告的样式弹出
oad.style.display = 'block';
//点击×的时候关闭
ob.onclick = function () {
oad.style.display = 'none';
clearInterval(t);
fn();//让点击×后,继续弹出。
}
//设置一个每秒自减1的定时器
// clearInterval(t)
t = setInterval(function () {
os.innerHTML--;
//当0秒的时候清除定时器,并且广告关闭
if (os.innerHTML == 0) {
clearInterval(t);
oad.style.display = 'none';
fn();//让自减这个定时器进行反复执行
}
}, 500)
}, 1000)
}
function $(id) {
return document.getElementById(id);
}
</script>
</body>
</html>