原生js对联广告代码(非flash对联广告)

发布时间:2019-09-19编辑:脚本学堂
纯js对联广告代码,带有关闭按钮的对联广告代码,网上找到的这段js对联广告代码,并在此基础上加以改进,以达到可以点击关闭的效果,需要的朋友参考下。

例1,js左右对联广告代码。

效果图:

js对联广告代码

代码:
 

复制代码 代码示例:
<!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>
<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*/}
 
//alert(diffY);
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;
//alert(lastScrollY);
}
suspendcode12="<DIV id="lovexin12" style='left:2px;POSITION:absolute;TOP:120px;'>ad1</div>"
document.write(suspendcode12);
window.setInterval("heartBeat()",1);
</script>
<style type="text/css">
<!--
#lovexin12,#lovexin14{
   width:120px;
   height:250px;
   background-color:#e5e5e5;
   border:1px solid #ddd;
}
html,body{
  height:1000px;
  }
#mm{
  height:1000px;
  }
-->
</style>
</head>
<body>
<div id="mm">
</div>
<div id="lovexin14" style="right:2px; position:absolute">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
  </tr>
</table>
</div>
</body>
</html>

带关闭按钮的对联广告

对这个“符合标准的对联广告”进行了修改,也加入了关闭按钮(实际是隐藏,不过效果一样),关于隐藏层,大致找到以下两个:
 

onclick="lovexin14.style.display = 'none'"
onclick="lovexin12.style.visibility='hidden'"

在点了关闭之后,会返回页面顶端,怎么去掉这种情况呢?
 

复制代码 代码示例:

<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*/}
    
//alert(diffY);
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; 
//alert(lastScrollY);
}
suspendcode12="<DIV id="lovexin12" style='left:5px;POSITION:absolute;TOP:120px;'><img border=0 src=http://www.jb51.net/logos.gif><br><a href=# onclick="lovexin12.style.visibility='hidden'">关闭</a></div>"
suspendcode14="<DIV id="lovexin14" style='right:5px;POSITION:absolute;TOP:120px;'><img border=0 src=http://www.jb51.net/logos.gif><br><a href=# onclick="lovexin14.style.display = 'none'">关闭</a></div>"
document.write(suspendcode12); 
document.write(suspendcode14); 
window.setInterval("heartBeat()",1);
</script>