支持IE6、7、Firefox并符合W3C标准的对联广告JS代码

经测,此JS对联广告代码支持IE6、7、Firefox、Opera等主流浏览器,且符合W3C标准。

一、准备好JS和图片文件

1、下载此图片:大小: 536 bytes
尺寸: 16 x 16
浏览: 436 次
点击打开新窗口浏览全图 放到该放的地方。

2、把以下代码保存为 ad.js 

JavaScript代码
  1. var lastScrollY=0;    
  2. function heartBeat(){   
  3. var diffY;    
  4. if (document.documentElement && document.documentElement.scrollTop)    
  5.     diffY = document.documentElement.scrollTop;    
  6. else if (document.body)    
  7.     diffY = document.body.scrollTop    
  8. else    
  9.     {/*Netscape stuff*/}    
  10.         
  11. percent=.1*(diffY-lastScrollY);     
  12. if(percent>0)percent=Math.ceil(percent);     
  13. else percent=Math.floor(percent);     
  14. document.getElementById("adleft").style.top=parseInt(document.getElementById("adleft").style.top)+percent+"px";    
  15. document.getElementById("adright").style.top=parseInt(document.getElementById("adright").style.top)+percent+"px";    
  16. lastScrollY=lastScrollY+percent;     
  17. }    
  18.   
  19. function adhide(names){document.getElementById(names).style.display='none';}   
  20. function screencl(names){if(screen.width<=800){adhide(names);}}   
  21. zcode="<div id='adleft' style='left:10px;position: absolute;z-index:1;top:100px;'><div style='width:100px;height:300px;background:#ccc;border:#999 solid 1px;'>左联</div><div align='right' style='background:#fff;font-size:0px;'> <img src='images/close.gif' title='关闭' onclick=adhide('adleft') style='cursor:pointer'></div></div>";   
  22. ycode="<div id='adright' style='right:10px;position: absolute;z-index:1;top:100px;'><div style='width:100px;height:300px;background:#ccc;border:#999 solid 1px;'>右联</div><div align='left' style='background:#fff;font-size:0px;'> <img src='images/close.gif' title='关闭' onclick=adhide('adright') style='cursor:pointer'></div></div>";   
  23. document.write(zcode);   
  24. document.write(ycode);   
  25. screencl('adleft');   
  26. screencl('adright');   
  27. window.setInterval("heartBeat()",1);   

二、使用说明

1、在网页中用 <script language="JavaScript" src="ad.js"></script> 调用即可。

2、把代码中的“左联”“右联”换成你自己的内容。

3、以前用 document.body 时,过不了W3C和FF这关,用 document.documentElement 代替OK。

如果只想要一联,在其相应 DIV 的 style='left:10px;position: absolute;z-index:1;top:100px;' 加入一句 display: none; 即可。

Trackbacks: 点击获得Trackback地址,Encode: UTF-8  点击获得Trackback地址,Encode: GB2312 or GBK  点击获得Trackback地址,Encode: BIG5
分类:CSS & JS
浏览概况:评论:0 | 阅读:9024
文章评论(0)
发表评论
用户名:
密码(游客无需):
网址/E-mail:
      记住我的信息