定时器原理关闭广告

定时器
setInterval()循环定时器 ,周而复始的执行
setTimeout()炸弹定时器,用完以后报废

var num=0;
  setInterval(function () {
      console.log(num);
      num++;
  },1000);

1.五秒关闭广告

 <script>
        window.function () {
            var imgArr=document.getElementsByTagName("img");
            setTimeout(fn,2000);
            function  fn() {
                imgArr[0].style.display="none";
                imgArr[1].style.display="none";
            }
        }
    </script>

2.模拟京东关闭广告

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding:0;
        }
        .site-nav{
            height:30px;
            background-color: #cccccc;
        }
        .top-banner{
            opacity: 1;
            background-color: blue;
        }
        .w{
            width:1210px;
            height: 80px;
            margin: 0px auto;
            background-color: pink;
        }
        .search{
            width:1210px;
            height: 80px;
            margin: 0px auto;
            background-color: burlywood;
        }
        a{
            position: absolute;
            top:40px;
            right:210px;
            width:25px;
            height: 25px;
            text-align: center;
            line-height: 25px;
            background-color: #000;
            color: #fff;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="site-nav"></div>
    <div class="top-banner">
        <div class="w">
            <a href="#">X</a>
        </div>
    </div>
    <div class="search">

    </div>
<script>
    var topBanner=document.getElementsByClassName("top-banner")[0];
    var a=topBanner.children[0].firstElementChild||topBanner.children[0].firstChild;
   var timer=null;
    a.function () {
        timer=setInterval(function () {
            topBanner.style.opacity-=0.1;
            if(topBanner.style.opacity<0){
                topBanner.style.display="none";
                clearInterval(timer);
            }
        },50);
    }
</script>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值