几秒后自动关闭广告
1.预期效果
效果描述:过5秒后广告自动关闭
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/baaac788d265ca7bc8553453392cdd11.gif#pic_center)
2.代码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.ad {
position: relative;
width: 132px;
height: 469px;
margin: 100px auto;
}
.ad p {
position: absolute;
top: 450px;
left: 48px;
font-size: 10px;
color: orange;
}
</style>
</head>
<body>
<div class="ad">
<img src="images/ad.jpg" alt="" class="ad_img">
<p></p>
</div>
<script>
var ad = document.querySelector('.ad');
var tip = document.querySelector('p');
var time = 5;
fn();
var timer = window.setInterval(fn,1000);
function fn(){
if(time == 0){
ad.style.display = 'none';
clearInterval(timer);
}
tip.innerHTML = time + '秒后自动关闭';
time--;
}
</script>
</body>
</html>