漂浮广告代码

2012-11-28  金城  4808

XHTML下运行

确认你的html页面有如下两行

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

将以下代码存入一个JS文件中,用<script type="javascript"src="你保存的.js"></srcipt>调用

//漂浮广告代码
(function(){
 var html='<a href="/"target="_blank"><img src="http://www.mycoolapp.net/images/logo.png"alt="软源"width="141"height="100"/></a>';//这里修改广告内容
 document.write('<div id="ff_0831224d7b9e40dabaccf3c3d51935e5">'+html+'<a id="ff_0831224d7b9e40dabaccf3c3d51935e5_ctrl"href="#close"onclick="return false">×</a></div>');
 var ff=document.getElementById('ff_0831224d7b9e40dabaccf3c3d51935e5');
 var D=document;var DE=D.documentElement;var A=3;
 ff.style.position='absolute';var ffw=ff.offsetWidth,ffh=ff.offsetHeight;
 var rw=DE.clientWidth-ffw,rh=DE.clientHeight-ffh;
 var ffl=Math.round(rw * Math.random()+DE.scrollLeft);ff.style.left=ffl+'px';
 var fft=Math.round((rh) * Math.random()+DE.scrollTop);ff.style.top=fft+'px';
 var ffxa=(Math.random()*A+.3),ffya=(Math.random()*A+.3);
 if(Math.random()>.5)ffxa=-ffxa;if(Math.random()>.5)ffya=-ffya;
 setInterval(function(){
  ffxa*=(1.5-Math.random());if(Math.abs(ffxa)<.1)ffxa=ffxa>0?.5:-.5;else if(Math.abs(ffxa)>A)ffxa=ffxa>0?A:-A;
  ffya*=(1.5-Math.random());if(Math.abs(ffya)<.1)ffxa=ffya>0?.5:-.5;else if(Math.abs(ffya)>A)ffya=ffya>0?A:-A;
 },3000);
 var floatfly=function(){
  rw=DE.clientWidth-ffw+DE.scrollLeft,rh=DE.clientHeight-ffh+DE.scrollTop;
  ffl+=ffxa;if(ffl>rw){ffl=rw-ffxa;ffxa=-ffxa}else if(ffl<DE.scrollLeft){ffl=DE.scrollLeft-ffxa;ffxa=-ffxa;}
  fft+=ffya;if(fft>rh){fft=rh-ffya;ffya=-ffya}else if(fft<DE.scrollTop){fft=DE.scrollTop-ffya;ffya=-ffya;}
  ff.style.left=Math.round(ffl)+'px';ff.style.top=Math.round(fft)+'px';
  ff.style.display='black';ff.style.visibility='visible';//简单防屏蔽
 }
 var hdl=setInterval(floatfly,100); 
 var ctrl=D.getElementById('ff_0831224d7b9e40dabaccf3c3d51935e5_ctrl');
 ctrl.onclick=function(){//关闭广告
  clearInterval(hdl);ff.style.display='none'
  floatfly=null;
  return false;
 }
 ctrl.onmouseover=function(){
  clearInterval(hdl);
 }
 ctrl.onmouseout=function(){
  clearInterval(hdl);
  hdl=setInterval(floatfly,100);
 }
 ff.onmouseover=function(){
  clearInterval(hdl);
 }
 ff.onmouseout=function(){
  clearInterval(hdl);
  hdl=setInterval(floatfly,100);
 }
}());