网盟飘底的JS广告是怎么写的?

作者:admin   时间:2020-08-03   访问量:133

  网盟的底部广告是一串通用的js代码,放到哪个网站都可以自然的实现广告插入,而且还不影响网页的显示。大树虽然没有仔细的研究过网盟的js,但是根据朋友的需求,写了一段js,基本上可以实现效果。不要小看这段代码哦,里面已经设置了pc跟手机的自适应效果,而且会自动判断图片的大小来调整宽高,里面还有点击“关闭”后随机(50%概率)跳转href的功能。
<script>
window.onload=function(){
    imgBottom(
        '.footer',
        'https://www.laihuihua.com/',
        'https://img.zcool.cn/community/0123935ba48366a8012099c88f8d65.jpg@1280w_1l_2o_100sh.jpg'
    );
}


function imgBottom(str,ahref,imgsrc){
    if(str[0]=='.'){
        str=str.substring(1);
        var oFoot=document.getElementsByClassName(str)[0];
    }else if(str[0]=='#'){
        str=str.substring(1);
        var oFoot=document.getElementById(str);
    }
    var footStyle=getStyle(oFoot,'position');
    function getStyle(obj, attr) {
        return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
    }
    
    var oA=document.createElement('a');
    oA.href=ahref;
    
    var oBtn=document.createElement('button');
    oBtn.innerHTML='关闭';
    oBtn.style.position='absolute';
    oBtn.style.top=0;
    oBtn.style.right=0;
    oBtn.style.zIndex=9999;
    oBtn.style.display='none';
    
    var oImg=new Image();
    oImg.src=imgsrc;
    var imgW=0;
    
    oA.appendChild(oBtn);
    oA.appendChild(oImg);
    document.body.appendChild(oA);

    oImg.onload=function(){
        oBtn.style.display='block';
        imgW=this.width;
        changeImg();
    }
    window.onresize=function(){
        changeImg();
    }
    
    function changeImg(){
        oA.style.display='block';
        oA.style.position='fixed';
        oA.style.bottom='0';
        if(document.body.clientWidth>imgW){
            oA.style.left=(document.body.clientWidth-imgW)/2+'px';
            oA.style.width=imgW+'px';
        }else{
            oA.style.left=0;
            oA.style.width='100%';        
        }
        oImg.style.width='100%';
        oImg.style.height='100%';
        oA.style.height='auto';
        oA.style.zIndex=5555;
        
        if(footStyle=='static'){
            oFoot.style.marginBottom=oA.offsetHeight+'px';
        }else{
            oFoot.style.position='relative';
            oFoot.style.bottom=oA.offsetHeight+'px';
        }
    }
    
    oBtn.onclick=function(){
        oA.style.display='none';
        if(footStyle=='static'){
            oFoot.style.marginBottom=0;
        }else{
            oFoot.style.bottom=0;
        }
        if(parseInt(Math.random()*100)%2==0){
            return false;
        }    
    }    
}
</script>

  imgBottom(str,ahref,imgsrc)的第一个参数str是用来写底部元素的class或者id,就是说这个广告图片需要插入到哪个元素的后面,就写上它的class或者id,class就用成".class",id就写成"#id"跟css的写法一样。第二个参数是你需要跳转的链接地址,第三个是广告图片的src。


【地址】:http://www.inseo.cn/seo/111.html转载请注明出处


相关文章

Copyright © 2018-2030 大树SEO All Rights Reserved.