0
点赞
收藏
分享

微信扫一扫

几秒后自动关闭广告(JS/DOM)

云上笔记 2022-03-11 阅读 52

几秒后自动关闭广告

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();
        window.setInterval(fn,1000);
        function fn(){
            if(time == 0){
                ad.style.display = 'none';
            }
            tip.innerHTML = time + '秒后自动关闭';
            time--;
        }     
    </script>
</body>
</html>
举报

相关推荐

0 条评论