本文主要是介绍几秒后自动关闭广告(JS/DOM),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
几秒后自动关闭广告
1.预期效果
效果描述:过5秒后广告自动关闭
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>
这篇关于几秒后自动关闭广告(JS/DOM)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!