js实现banner广告和插屏广告

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js实现banner和插屏广告</title>
    <style>
        input:first-child{
            margin-top: 80px;
            border: 1px solid #ccc;
        }
        input{
            width: 500px;
            height: 80px;
            border-radius: 50px;
            font-size: 40px;
            font-weight: bold;
            text-align: center;
            line-height: 50px;
            letter-spacing: 2px;
            display: block;
            margin: 20px auto;
            background: #ccc;
            cursor: pointer;
        }
    </style>
    <script>
        function bannerView(){
            var banner=document.getElementsByTagName("div")[0];
            banner.style.display="block";

        }
    </script>
    <script type="text/javascript">
        function advert(config){
        }
        advert.prototype={
            constructor: advert,
            adsBox:function(){
                var that=this;
                     var boxWidth;
                    // 与屏幕大小比较
                    if(this.width>=screen.width){
                        boxWidth="95%";
                    }else{
                        boxWidth=that.width;
                    }
                    var ad=document.createElement("div");
                    ad.setAttribute("style","z-index:1000000;position: fixed;top:0;left:0;");
                    //广告主体内容
                    var html1=`<div class="cover" style="width: 100%;height: 100%;background: rgba(0,0,0,0.3);position: fixed;top: 0;left: 0;"></div>`
                        +`<div id="insertBox" style="position: fixed; width: `+boxWidth+`;height:auto;top: 50%;left: 50%;transform:translate(-50%,-50%);-webkit-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);-o-transform:translate(-50%,-50%);z-index:20;">`
                        +`<a href="#" style="display:block;width:100%;height:auto"><img src="https://s3m.mediav.com/galileo/2a74d406010b239591bd7bd442422d3f.png" alt="图片" style="width:100%;height:auto;"></a>`
                        +`<span id="insert_closeBtn" style="position: absolute;cursor:pointer;top: 10px;right: 10px;width: 32px;height: 32px;border-radius: 16px;background: rgba(0,0,0,0.5);color: #FFF;text-align: center;line-height: 26px;font-size:32px;font-family:'MingLiU';">&times;</span>`
                        +`</div>`;
                    ad.innerHTML=html1;
                    document.body.appendChild(ad);
                    var box=document.getElementById("insertBox");
                    var closeBtn=document.getElementById("insert_closeBtn")
                    //删除
                    closeBtn.onclick=function(){
                        document.body.removeChild(ad);
                    }
                },
            // 插屏广告
            interstitialAd:function () {
                console.log(1);
                var that=this;
                var img = new Image();
                img.src = "https://s3m.mediav.com/galileo/2a74d406010b239591bd7bd442422d3f.png";
                if (img.complete) {
                    that.width=img.width;
                    that.height=img.height;
                    that.adsBox();
                } else {
                    // 判断图片加载完成时获取到图片的宽高
                    img.onload = function () {
                        that.width=img.width;
                        that.height=img.height;
                        img.onload = null;
                        that.adsBox();
                    };
                };
                return this;
            },
            // banner广告
            banner:function(){
                // 渲染页面
                var html='<div style="cursor: pointer;z-index: 10000;width: 300px;height: 100px">'
                +'<a href="#" style="display: block;width:100%;height: auto">'
                +'<img src="https://p0.ssl.qhimg.com/t01cc456dbd8c4fdb8d.png" style="width: 300px;height: 100px;" alt="图片信息"></a></div>';
                document.writeln(html); 
            }
        }
    </script>
    <script type="text/javascript">
        function insertAdv(){
            new advert().interstitialAd();  
        }     
    </script>
</head>
<body>
    <input value="插屏广告(点我)" onclick="insertAdv()" type="btn"/>
    <input value="banner广告(点我)" onclick="bannerView()" type="btn"/>
    <!-- banner广告位1 -->
    <div style="display: none;">
    <h3>广告位1</h3>
        <script>
            new advert({
            width:"1000px",
            height:"300px"
            }).banner();  
        </script>
    </div>
</body>
</html>

 

转载于:https://www.cnblogs.com/zhougeBlog/p/6872497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值