整个广告放在一个js文件中,所以,需要先在模板上引入这个baidu.js文件
广告位置会自动插入,一般不需要找位置.
因为js中有一个固定位置的广告,所以.哪个位置需要显示这个固定广告,则直接把js放在相应位置即可.放好后,其它的对联广告等会自动显示
baidu.js先引入
<div class="abc"><script src="js/baidu.js"></script></div>
baidu.js中的内容为
var top2 = "https://cuntuku.com/images/2018/07/21/txmg7.gif";
//手机端头部,不显示时,可能变量冲突, 可以换个变量名
var left = 'http://wx2.sinaimg.cn/mw690/0060lm7Tly1ftgf963phwj30b40hk7ox.jpg' //左侧对联图片
var right = 'http://wx1.sinaimg.cn/orj480/0060lm7Tly1ftgbq3dpsej30b40hktt3.jpg' //右侧对联图片
var auto = 'https://cuntuku.com/images/2018/07/21/txmg7.gif' // pc固定
var pageWidth = 980; //页面内容的宽度
var slidWidth = 200; //左右两侧的图片宽度
var slidHeight = 300; //左右两侧的图片高度
var autoHeight = 220; //固定图片高度
var autoTop = 0; //固定图片距离顶部的位置
var phoneHeight = 150; //手机顶部图片高度
var link1 = 'http://www.yabovip55.cc'; //手机顶部链接
var link2 = 'http://www.yabo2018.net/?i_code=20082'; //对联左侧链接
var link3 = 'http://www.yabo2018.net/?i_code=20082'; //对联右侧链接
var link4 = 'http://www.yabo2018.net/?i_code=20082'; //固定位置链接
var style = document.createElement('style');
style.innerHTML = '#fixed-left,#fixed-right{position: fixed;width:'+ slidWidth +'px;height: '+ slidHeight +'px;margin-top: -'+ slidHeight/2 +'px;z-index:99999;top:50%;}#fixed-left{left:50%;margin-left:-'+ (pageWidth/2+slidWidth) +'px;}#fixed-right{right:50%;margin-right:-'+ (pageWidth/2+slidWidth) +'px;}#fixed-left img,#fixed-top img,#fixed-right img,#fixed-auto img{display: block;width:100%;height: 100%;}#fixed-top,{position: fixed;width: '+ pageWidth +'px;left:50%;margin-left: -'+ pageWidth/2 +'px;}#fixed-top{top:0;height: '+phoneHeight+'px}#fixed-auto{width: 100%;height:'+ autoHeight +'px}.fclose{position: absolute;right: 0;top: 0;background: red;color:#fff;padding:2px 3px;font-size: 14px;}';
document.head.appendChild(style);
//对联广告
var fixedleftright = document.createElement('div');
fixedleftright.innerHTML = '<div id="fixed-left"><a href="'+ link2 +'"><img src="'+ left +'" alt=""></a><div id="fclose-left" class="fclose">关闭</div></div><div id="fixed-right"><a href="'+ link3 +'"><img src="'+ right +'" alt=""></a>\
<div id="fclose-right" class="fclose">关闭</div></div>';
document.getElementsByTagName('body')[0].appendChild(fixedleftright);
//顶部广告,手机专用
var fixedtop = document.createElement('div');
fixedtop.innerHTML = '<div id="fixed-top"><a href="'+ link1 +'"><img src="'+ top2 +'" alt=""></a><div id="fclose-top" class="fclose">关闭</div></div>';
document.getElementsByTagName('body')[0].prepend(fixedtop);
// 固定广告
fixedauto = '<div id="fixed-auto"><a href="'+link4+'"><img src="'+ auto +'" alt=""></a><div id="fclose-bottom" class="fclose"></div></div>';
document.writeln(fixedauto);//直接在引入位置显示固定广告
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {//在手机上,对联及固定广告都隐藏
document.getElementById('fixed-left').style.display = "none";
document.getElementById('fixed-right').style.display = "none";
document.getElementById('fixed-auto').style.display = "none";
} else {//电脑上,顶部广告直接隐藏
document.getElementById('fixed-top').style.display = "none";
}
var fclose = document.getElementsByClassName('fclose');
for (var i = 0; i < fclose.length; i++){
fclose[i].onclick = function(){
this.parentNode.style.display = "none";
}
}