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

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

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

1.htm








liangbian.js
//左边
var left_img='

'; //上边是两个左边的图片效果,不够可以继续加,代码就是“
” //右边 var right_img='

'; //var right_img=''; var delta=0.05 var collection; function floaters() { this.items = []; this.addItem = function(id,x,y,content) { document.write('
'+content+'
'); 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;i0?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('
'); if (isflash>0) { document.write(""); } else { document.write(""); document.write("" + alt + ""); } document.write("
"); ads();





你可能感兴趣的:(漂浮广告代码、漂浮代码分析(讲解))