漂浮广告代码、漂浮代码分析(讲解)

总共有三个文件1.htm 主文件、liangbian.js 在两边漂浮的代码 、piaofu.js在全屏幕漂浮的代码
把他们的分别保存成三个文件,放在一个目录下,双击1.htm就可以看到效果了.

大家可以根据需要更换图片和链接。

1.htm
<html>
<body link="#000000" vlink="#000000">
<script language=JavaScript src="liangbian.js"></script>
<script language=JavaScript src="piaofu.js"></script>
</body>
</html>



liangbian.js
//左边
var left_img='<a href=http://www.baidu.com/ target=_blank><img src=1.gif border=0></a><br><br><a href="http://www.google.com" target=_blank><img src=2.gif border=0></a>';

//上边是两个左边的图片效果,不够可以继续加,代码就是“<br><a href="http://www.google.com" target=_blank><img src=2.gif border=0></a>”

//右边
var right_img='<a href=http://www.baidu.com.cn target=_blank><img src=1.gif border=0></a><br><br><a href="http://www.google.com/" target=_blank><img src=2.gif border=0></a>';
//var right_img='<EMBED src=1.swf quality=high  WIDTH=80 HEIGHT=170 TYPE="application/x-shockwave-flash"></EMBED>';
 var delta=0.05
 var collection;
 function floaters() {
  this.items = [];
  this.addItem = function(id,x,y,content)
      {
     document.write('<DIV id='+id+' style="Z-INDEX: 10; POSITION: absolute;  width:80px; height:60px;left:'+(typeof(x)=='string'?eval(x):x)+';top:'+(typeof(y)=='string'?eval(y):y)+'">'+content+'</DIV>');
    
     var newItem    = {};
     newItem.object   = document.getElementById(id);
     newItem.x    = x;
     newItem.y    = y;

     this.items[this.items.length]  = newItem;
      }
  this.play = function()
      {
     collection    = this.items
     setInterval('play()',10);
      }
  }
  function play()
  {

   for(var i=0;i<collection.length;i++)
   {
    var followObj  = collection[i].object;
    var followObj_x  = (typeof(collection[i].x)=='string'?eval(collection[i].x):collection[i].x);
    var followObj_y  = (typeof(collection[i].y)=='string'?eval(collection[i].y):collection[i].y);

    if(followObj.offsetLeft!=(document.body.scrollLeft+followObj_x)) {
     var dx=(document.body.scrollLeft+followObj_x-followObj.offsetLeft)*delta;
     dx=(dx>0?1:-1)*Math.ceil(Math.abs(dx));
     followObj.style.left=followObj.offsetLeft+dx;
     }

    if(followObj.offsetTop!=(document.body.scrollTop+followObj_y)) {
     var dy=(document.body.scrollTop+followObj_y-followObj.offsetTop)*delta;
     dy=(dy>0?1:-1)*Math.ceil(Math.abs(dy));
     followObj.style.top=followObj.offsetTop+dy;
     }
    followObj.style.display = '';
   }
  }
 
 var theFloaters  = new floaters();
 theFloaters.addItem('followDiv1','document.body.clientWidth-100',230,''+right_img+'');
 theFloaters.addItem('followDiv2',20,230,''+left_img+'');
 theFloaters.play();






piaofu.js
//可设置漂浮图片是否为FLASH以及显示大小
var brOK=false;
var mie=false;
var vmin=2;
var vmax=5;
var vr=1;
var timer1;
var ads;
var isflash=0;      //是否为FLASH文件的漂浮 1=是 0=否
var pic="1.gif";     //图片的地址
var alt="请点我吧!";     //鼠标放在图片上时显示的提示文字
var url="http://www.google.com/";  //图片链接的地址
var flashurl="1.swf";   //FLASH文件的路径
var Wimg=80;     //图像的宽度
var Himg=80;     //图像的高度
function movechip(chipname)
{
 if(brOK)
 {
  eval("chip="+chipname);
  if(!mie)
  {
   pageX=window.pageXOffset;
   pageW=window.innerWidth;
   pageY=window.pageYOffset;
   pageH=window.innerHeight;
  }
  else
  {
   pageX=window.document.body.scrollLeft;
   pageW=window.document.body.offsetWidth-8;
   pageY=window.document.body.scrollTop;
   pageH=window.document.body.offsetHeight;
  }
  chip.xx=chip.xx+chip.vx;
  chip.yy=chip.yy+chip.vy;
  chip.vx+=vr*(Math.random()-0.5);
  chip.vy+=vr*(Math.random()-0.5);
  if(chip.vx>(vmax+vmin))  chip.vx=(vmax+vmin)*2-chip.vx;
  if(chip.vx<(-vmax-vmin)) chip.vx=(-vmax-vmin)*2-chip.vx;
  if(chip.vy>(vmax+vmin))  chip.vy=(vmax+vmin)*2-chip.vy;
  if(chip.vy<(-vmax-vmin)) chip.vy=(-vmax-vmin)*2-chip.vy;
  if(chip.xx<=pageX)
  {
   chip.xx=pageX;
   chip.vx=vmin+vmax*Math.random();
  }
  if(chip.xx>=pageX+pageW-chip.w)
  {
   chip.xx=pageX+pageW-chip.w;
   chip.vx=-vmin-vmax*Math.random();
  }
  if(chip.xx>=680)
  {
   chip.xx=chip.xx-20;
   chip.vx=-vmin-vmax*Math.random();
  }
  if(chip.yy<=pageY)
  {
   chip.yy=pageY;
   chip.vy=vmin+vmax*Math.random();
  }
  if(chip.yy>=pageY+pageH-chip.h)
  {
   chip.yy=pageY+pageH-chip.h;
   chip.vy=-vmin-vmax*Math.random();
  }
  if(!mie)
  {
   eval('document.'+chip.named+'.top ='+chip.yy);
   eval('document.'+chip.named+'.left='+chip.xx);
  }
  else
  {
   eval('document.all.'+chip.named+'.style.pixelLeft='+chip.xx);
   eval('document.all.'+chip.named+'.style.pixelTop ='+chip.yy);
  }
  chip.timer1=setTimeout("movechip('"+chip.named+"')",80);
 }
}

function stopme(chipname)
{
 if(brOK)
 {
  eval("chip="+chipname);
  if(chip.timer1!=null)
  {
   clearTimeout(chip.timer1)
  }
 }
}

function ads()
{
 if(navigator.appName.indexOf("Internet Explorer")!=-1)
 {
  if(parseInt(navigator.appVersion.substring(0,1))>=4) brOK=navigator.javaEnabled();mie=true;
 }
 if(navigator.appName.indexOf("Netscape")!=-1)
 {
  if(parseInt(navigator.appVersion.substring(0,1))>=4) brOK=navigator.javaEnabled();
 }
 ads.named="ads";
 ads.vx=vmin+vmax*Math.random();
 ads.vy=vmin+vmax*Math.random();
 ads.w=1;
 ads.h=1;
 ads.xx=0;
 ads.yy=0;
 ads.timer1=null;
 movechip("ads");
}

document.write('<div id="ads" onmouseover=stopme("ads"); onmouseout=movechip("ads"); style="height:49px;left:178px;position:absolute;top:1237px;width:70px; z-index:1000">');
if (isflash>0)
{
document.write("<EMBED src='" + flashurl + "' quality=high  WIDTH='" + Wimg + "' HEIGHT='" + Himg + "' TYPE='application/x-shockwave-flash'></EMBED>");
}
else
{
document.write("<a href='" + url + "' target=_blank>");
document.write("<img src='" + pic + "' alt='" + alt + "' border=0 width='" + Wimg + "' height='" + Himg + "'></a>");
}
document.write("</div>");
ads();





你可能感兴趣的:(代码)