在很多时候我们想为自己的站点添加一些广告,除了常见的固定广告位,使用最多的也就是顶部悬浮或者底部悬浮甚至PC端的对联或者漂浮广告,等等花式广告。

网上一堆代码,但不一定你能搜到~这不你正好搜到了这篇文章。废话不说啦开始干饭吧!

固定顶部全局图片代码

<script>
document.write("<style>");
document.write("#topNavad{background-color:#fff0; z-index:999; position:fixed; top:0; left:0; width:100%; _position:absolute;");
document.write("_top: expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight); overflow:visible; }");
document.write("#bottomNavad{background-color:#fff0; z-index:999; position:fixed; bottom:0; left:0; width:100%; _position:absolute;");
document.write("_top: expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight); overflow:visible; }");
document.write("<\/style>");
document.write("");
document.write(" ");
document.write("<!--头部-->");
document.write("<div id=\"topNavad\">");
document.write("<div id=\"MyDiv2\">");
document.write("<div id=\"newsImg\">");
document.write("<center>");
document.write("<a href=\"https:\/\/www.baidu.com\" rel=\"nofollow\" target=\"_blank\">");
document.write("<img src=\"http://cdn.u1.huluxia.com/g4/M02/02/80/rBAAdmBBaJKAKPyoABiDD1McAg0519.png\" title=\"AD\" \/ style=\"width:100%\">");
document.write("<\/a>");
document.write("<\/center>");
document.write("<\/div>");
document.write("<span onclick='CloseDiv(\"MyDiv2\")' style=\"text-align:right; display:block;\">关闭<\/span>");
document.write("");
document.write("<\/div>");
document.write("<\/div>");
document.write("<!--头部end-->");
document.write("");
function CloseDiv(div) {
document.getElementById(div).style.display = 'none';
};
</script>

固定底部全局图片代码

<script>
document.write("<style>");
document.write("#topNavad{background-color:#fff0; z-index:999; position:fixed; top:0; left:0; width:100%; _position:absolute;");
document.write("_top: expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight); overflow:visible; }");
document.write("#bottomNavad{background-color:#fff0; z-index:999; position:fixed; bottom:0; left:0; width:100%; _position:absolute;");
document.write("_top: expression(documentElement.scrollTop + documentElement.clientHeight-this.offsetHeight); overflow:visible; }");
document.write("<\/style>");
document.write("");
document.write("");
document.write("<!--底部-->");
document.write("<div id=\"bottomNavad\">");
document.write("<div id=\"MyDiv1\">");
document.write("<span onclick='CloseDiv(\"MyDiv1\")' style=\"text-align:right; display:block;\">关闭<\/span>");
document.write("<center>");
document.write("<a href=\"https:\/\/www.yijuv.cn\" rel=\"nofollow\" target=\"_blank\">");
document.write("<img src=\"https://kodo.20sl.cn/so1oab9s/04618b76af2b3.png\" title=\"AD\" \/ style=\"width:100%\">");
document.write("<\/a>");
document.write("<\/center>");
document.write("<\/div>");
document.write("<\/div>");
document.write("<!--底部end-->");
document.write("");
function CloseDiv(div) {
document.getElementById(div).style.display = 'none';
};
</script>

其它广告代码分享

随滚动而动标准对联广告

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>标准对联广告Js代码--信封生活网</title>
<style type="text/css">
<!--
#lovexin12,#lovexin14{
width:90px;
height:230px;
background-color:#ededed;
border:1px solid #ddd;
}
html,body{
height:1203px;
}
#mm{
height:1000px;
}
-->
</style>

<script language="JavaScript" type="text/javascript">
lastScrollY=0;
function heartBeat(){ 
var diffY;
if (document.documentElement && document.documentElement.scrollTop)
diffY = document.documentElement.scrollTop;
else if (document.body)
diffY = document.body.scrollTop
else
{/*Netscape stuff*/}
percent=.1*(diffY-lastScrollY); 
if(percent>0)percent=Math.ceil(percent); 
else percent=Math.floor(percent); 
document.getElementById("lovexin12").style.top=parseInt(document.getElementById
("lovexin12").style.top)+percent+"px";
document.getElementById("lovexin14").style.top=parseInt(document.getElementById
("lovexin12").style.top)+percent+"px";
lastScrollY=lastScrollY+percent; 
}
suspendcode12="<DIV id=\"lovexin12\" style='left:2px;POSITION:absolute;TOP:120px;'>ad1</div>"
suspendcode14="<DIV id=\"lovexin14\" style='right:2px;POSITION:absolute;TOP:120px;'>ad2</div>"
document.write(suspendcode12); 
document.write(suspendcode14); 
window.setInterval("heartBeat()",1);
</script>
</head>
<body>
<div id="mm"></div>
</body>
</html>
<html>
<head>
<title>对联广告可关闭--信封生活网</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body style="margin:0px;">
<div align="center">
  <center>
  <table border="1" width="776" height="3000" cellspacing="0" cellpadding="0">
    <tr>
      <td width="100%" valign="top"><div align="center">页面区域</div></td>
    </tr>
  </table>
  </center>
</div>
<SCRIPT LANGUAGE="JavaScript">
<!--
var showad = true;
var Toppx = 60;//上端位置
var AdDivW = 100;//宽度
var AdDivH = 360;//高度
var PageWidth = 800;//这个参数决定是否出现左右滚动条
var MinScreenW = 1024;	//显示广告的最小屏幕宽度象素
var ClosebuttonHtml = '<div align="right" style="position: absolute;top:0px;right:0px;margin:2px;padding:2px;z-index:2000;"><a href="javascript:;" onclick="hidead()" style="color:red;text-decoration:none;font-size:12px;">关闭</a></div>'
var AdContentHtml = '<div align="center"><br><br>广<br>告<br>内<br>容</div>';
document.write ('<div id="Javascript.LeftDiv" style="position: absolute;border: 1px solid #336699;background-color:#EEEEE2;z-index:1000;width:'+AdDivW+'px;height:'+AdDivH+'px;top:-1000px;word-break:break-all;display:none;">'+ClosebuttonHtml+'<div>'+AdContentHtml+'</div></div>');
document.write ('<div id="Javascript.RightDiv" style="position: absolute;border: 1px solid #336699;background-color:#EEEEE2;z-index:1000;width:'+AdDivW+'px;height:'+AdDivH+'px;top:-1000px;word-break:break-all;display:none;">'+ClosebuttonHtml+'<div>'+AdContentHtml+'</div></div>');
function scall(){
	if(!showad){return;}
	if (window.screen.width<MinScreenW){
		alert("临时提示:\n\n显示器分辨率宽度小于"+MinScreenW+",不显示广告");
		showad = false;
		document.getElementById("Javascript.LeftDiv").style.display="none";
		document.getElementById("Javascript.RightDiv").style.display="none";
		return;
	}
	var Borderpx = ((window.screen.width-PageWidth)/2-AdDivW)/2;

	document.getElementById("Javascript.LeftDiv").style.display="";
	document.getElementById("Javascript.LeftDiv").style.top=document.body.scrollTop+Toppx;
	document.getElementById("Javascript.LeftDiv").style.left=document.body.scrollLeft+Borderpx;
	document.getElementById("Javascript.RightDiv").style.display="";
	document.getElementById("Javascript.RightDiv").style.top=document.body.scrollTop+Toppx;
	document.getElementById("Javascript.RightDiv").style.left=document.body.scrollLeft+document.body.clientWidth-document.getElementById("Javascript.RightDiv").offsetWidth-Borderpx;
}

function hidead()
{
	showad = false;
	document.getElementById("Javascript.LeftDiv").style.display="none";
	document.getElementById("Javascript.RightDiv").style.display="none";
}
window.onscroll=scall;
window.onresize=scall;
window.onload=scall;
//-->
</SCRIPT>
</body>
</html>

弹出广告/公告定时关闭

<html>
<head>
<title>层弹出定时关闭代码--信封生活网</title>
<style type="text/css">
<!--
#sponsorAdDiv {position:absolute; height:1; width:1; top:0; left:0;}
-->
</style>
<SCRIPT LANGUAGE="JavaScript1.2">
adTime=5; 
chanceAd=1;
var ns=(document.layers);
var ie=(document.all);
var w3=(document.getElementById  &&  !ie);
adCount=0;
function initAd(){
        if(!ns  &&  !ie  &&  !w3) return;
        if(ie)                adDiv=eval('document.all.sponsorAdDiv.style');
        else if(ns)        adDiv=eval('document.layers["sponsorAdDiv"]');
        else if(w3)        adDiv=eval('document.getElementById("sponsorAdDiv").style');
        randAd=Math.ceil(Math.random()*chanceAd);
        if (ie||w3)
        adDiv.visibility="visible";
        else
        adDiv.visibility ="show";
        if(randAd==1) showAd();
}
function showAd(){
if(adCount<adTime*10){adCount+=1;
        if (ie){documentWidth  =document.body.offsetWidth/2+document.body.scrollLeft-20;
        documentHeight =document.body.offsetHeight/2+document.body.scrollTop-20;}
        else if (ns){documentWidth=window.innerWidth/2+window.pageXOffset-20;
        documentHeight=window.innerHeight/2+window.pageYOffset-20;}
        else if (w3){documentWidth=self.innerWidth/2+window.pageXOffset-20;
        documentHeight=self.innerHeight/2+window.pageYOffset-20;}
        adDiv.left=documentWidth-200;adDiv.top =documentHeight-200;
        setTimeout("showAd()",100);}else closeAd();
}
function closeAd(){
if (ie||w3)
adDiv.display="none";
else
adDiv.visibility ="hide";
}
onload=initAd;
</script>
</head>

<body>
<div id="sponsorAdDiv" style="visibility:hidden">
<table width="412" height="288" bgcolor="red"><tr><td>
<table width="407" height="288" bgcolor="F0FFF0"><tr><td>
<center>Welcome To SITE<BR>本窗口会在5秒后自动关闭</center>
</td></tr></table></td></tr></table></div>
</body>
</html>

顶部展开后自动关闭的广告

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>可以展开、收缩的顶部滑动广告--信封生活网</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<style type="text/css">
body{margin:0;}
</style>
<script type="text/javascript"> 
var intervalId = null; 
function slideAd(id,nStayTime,sState,nMaxHth,nMinHth){ 
  this.stayTime=nStayTime*1000 || 3000; 
  this.maxHeigth=nMaxHth || 90; 
  this.minHeigth=nMinHth || 1; 
  this.state=sState || "down" ; 
  var obj = document.getElementById(id); 
  if(intervalId != null)window.clearInterval(intervalId); 
  function openBox(){ 
   var h = obj.offsetHeight; 
   obj.style.height = ((this.state == "down") ? (h + 2) : (h - 2))+"px"; 
    if(obj.offsetHeight>this.maxHeigth){ 
    window.clearInterval(intervalId); 
    intervalId=window.setInterval(closeBox,this.stayTime); 
    } 
    if (obj.offsetHeight<this.minHeigth){ 
    window.clearInterval(intervalId); 
    obj.style.display="none"; 
    } 
  } 
  function closeBox(){ 
   slideAd(id,this.stayTime,"up",nMaxHth,nMinHth); 
  } 
  intervalId = window.setInterval(openBox,10); 
} 
</script> 
</head>
<body>
<div id="MyMoveAd" style="background:skyblue;height:12px;text-align:center;overflow:hidden;">这里放广告内容哦!</div>
<script type="text/javascript"> 
<!-- 
 slideAd('MyMoveAd',2); 
--> 
</script>
</body>
</html>

右下角可小化可关闭自动的浮窗代码

<html>
<head>
<title>右下角广告代码--信封生活网</title>
<style type="text/css">
#msg_win{border:1px solid #A67901;background:#EAEAEA;width:300px;position:absolute;right:2;margin:0px;display:none;overflow:hidden;z-index:99;}
#msg_win .icos{position:absolute;top:2px;*top:0px;right:2px;z-index:9;}
.icos a{float:left;color:#833B02;margin:1px;text-align:center;text-decoration:none;font-family:webdings;}
.icos a:hover{color:#fff;}
#msg_title{background:#FECD00;border-bottom:1px solid #A67901;border-top:1px solid #FFF;border-left:1px solid #FFF;color:#000;height:25px;line-height:25px;text-indent:5px;}
#msg_content{margin:2px;width:300px;height:200px;overflow:hidden;}
</style>
</head>
<body>
<p style="height:1000px;"></p>
<div id="msg_win" style="display:block;top:490px;visibility:visible;opacity:1;">
<div class="icos"><a id="msg_min" title="最小化" href="javascript:void 0">_</a><a id="msg_close" title="关闭" href="javascript:void 0">×</a></div>
<div id="msg_title">广而告之:</div>
<div id="msg_content"><a href="http://www.20sl.cn/" target="_blank"><img src="/ad.gif" width="300" height="270" border="0"></a></div>
</div>
</body>
</html>
<script language="javascript">
var Message={
set: function() {//最小化与恢复状态切换
var set=this.minbtn.status == 1?[0,1,'block',this.char[0],'最小化']:[1,0,'none',this.char[1],'恢复'];
this.minbtn.status=set[0];
this.win.style.borderBottomWidth=set[1];
this.content.style.display =set[2];
this.minbtn.innerHTML =set[3]
this.minbtn.title = set[4];
this.win.style.top = this.getY().top;
},
close: function() {//关闭
this.win.style.display = 'none';
window.onscroll = null;
},
setOpacity: function(x) {//设置透明度
var v = x >= 100 ? '': 'Alpha(opacity=' + x + ')';
this.win.style.visibility = x<=0?'hidden':'visible';//IE有绝对或相对定位内容不随父透明度变化的bug
this.win.style.filter = v;
this.win.style.opacity = x / 100;
},
show: function() {//渐显
clearInterval(this.timer2);
var me = this,fx = this.fx(0, 100, 0.1),t = 0;
this.timer2 = setInterval(function() {
t = fx();
me.setOpacity(t[0]);
if (t[1] == 0) {clearInterval(me.timer2) }
},10);
},
fx: function(a, b, c) {//缓冲计算
var cMath = Math[(a - b) > 0 ? "floor": "ceil"],c = c || 0.1;
return function() {return [a += cMath((b - a) * c), a - b]}
},
getY: function() {//计算移动坐标
var d = document,b = document.body, e = document.documentElement;
var s = Math.max(b.scrollTop, e.scrollTop);
var h = /BackCompat/i.test(document.compatMode)?b.clientHeight:e.clientHeight;
var h2 = this.win.offsetHeight;
return {foot: s + h + h2 + 2+'px',top: s + h - h2 - 2+'px'}
},
moveTo: function(y) {//移动动画
clearInterval(this.timer);
var me = this,a = parseInt(this.win.style.top)||0;
var fx = this.fx(a, parseInt(y));
var t = 0 ;
this.timer = setInterval(function() {
t = fx();
me.win.style.top = t[0]+'px';
if (t[1] == 0) {
clearInterval(me.timer);
me.bind();
}
},10);
},
bind:function (){//绑定窗口滚动条与大小变化事件
var me=this,st,rt;
window.onscroll = function() {
clearTimeout(st);
clearTimeout(me.timer2);
me.setOpacity(0);
st = setTimeout(function() {
me.win.style.top = me.getY().top;
me.show();
},600);
};
window.onresize = function (){
clearTimeout(rt);
rt = setTimeout(function() {me.win.style.top = me.getY().top},100);
}
},
init: function() {//创建HTML
function $(id) {return document.getElementById(id)};
this.win=$('msg_win');
var set={minbtn: 'msg_min',closebtn: 'msg_close',title: 'msg_title',content: 'msg_content'};
for (var Id in set) {this[Id] = $(set[Id])};
var me = this;
this.minbtn.onclick = function() {me.set();this.blur()};
this.closebtn.onclick = function() {me.close()};
this.char=navigator.userAgent.toLowerCase().indexOf('firefox')+1?['_','::','×']:['0','2','r'];//FF不支持webdings字体
this.minbtn.innerHTML=this.char[0];
this.closebtn.innerHTML=this.char[2];
setTimeout(function() {//初始化最先位置
me.win.style.display = 'block';
me.win.style.top = me.getY().foot;
me.moveTo(me.getY().top);
},0);
return this;
}
};
Message.init();
</script>