javascript 实现飘动的广告(网络收藏,转)
1、javascript代码
2、调用页面
只要在body加载的时候调用该函数就可以了。
// JScript 文件
var pic="images/dragon.swf";
var alt="查看奖品情况";
var gourl="bbs/index.asp"; //点击后连接的地址
var speed=100;//速度调整,越小越快
var light=0;//如果是1的话就自动改变透明度,否则不改变透明度
var stop=0;
var num=0;
var brOK=false;
var mie=false;
var aver=parseInt(navigator.appVersion.substring(0,1));
var aname=navigator.appName;
var vmin=2;
var vmax=5;
var vr=2;
var timer1;
var allyes;
var ns = (document.layers) ? 1 : 0;
var ie = (document.all) ? 1 : 0;
//var ie=0;
//var ns=1;
var bFade=false;
var photoname=1;
var counts=0;
function checkbrOK(){
if(aname.indexOf("Internet Explorer")!=-1){
if(aver>=4) brOK=navigator.javaEnabled();
mie=true;
}
if(aname.indexOf("Netscape")!=-1){
if(aver>=4) brOK=navigator.javaEnabled();
}
}
function Chip(chipname,width,height){
this.named=chipname;
this.vx=vmin+vmax*Math.random();
this.vy=vmin+vmax*Math.random();
this.w=width;
this.h=height;
this.xx=10;
this.yy=10;
this.timer1=null;
}
function movechip(chipname){
if (stop!=1){
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;
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.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);
}
//var temp=parseInt(chip.yy%4);
// if(temp!=0)
// document.all("oimg2").src=temp+".gif";
chip.timer1=setTimeout("movechip('"+chip.named+"')",speed);
}
}
else {chip.timer1=setTimeout("movechip('"+chip.named+"')",speed);}
if (ie){
if (light==1){
var nOpacity=oImg.filters.alpha.opacity;
if (nOpacity>=100) bFade=true;
if (nOpacity<=0) bFade=false;
if (bFade) oImg.filters.alpha.opacity--;
if (!bFade) oImg.filters.alpha.opacity++;
}
}
}
function stopme(chipname){
if(brOK){
eval("chip="+chipname);
if(chip.timer1!=null){
clearTimeout(chip.timer1)
}
}
}
function allyes1(){
if (num==0){
checkbrOK();
allyes=new Chip("allyes",100,80);
if(brOK) {
movechip("allyes");
}
}
num++;
}
if (ie){
document.write("<div ID='allyes' STYLE='position:absolute;left: 10px; top: 10px;'>");
document.write("<a HREF='"+gourl+"' target=_blank>");
if (light==1){
document.write("<dd><img SRC='" + pic + "' onload='allyes1()' ID='oImg' border='0' alt='" + alt + "' style='position:absolute;left:200px;top:200px;filter:alpha(opacity=50);' onMouseOver='stop=1;' onMouseOut='stop=0;'></a></dd>");
}
else if (light==0){
//document.write("<dd><div ><img id='oimg2' SRC='" + pic + "' border='0' alt='" + alt + "' onload='allyes1()' onMouseOver='stop=1;' onMouseOut='stop=0;'></div></a></dd>");
document.write("<dd><div id='myphoto' style='position: absolute;width:90;top:20;left:2;visibility: visible;z-index: 1' onMouseOver='stop=1;' onMouseOut='stop=0;'><EMBED src='"+pic+"' quality=high WIDTH=160px HEIGHT=160px TYPE='application/x-shockwave-flash' id=dragon wmode='transparent'></EMBED></div></dd></a>");
}
document.write("</div>");
}
else if (ns){
document.write("<layer name='allyes' visibility='1' onMouseOver='stop=1;' onMouseOut='stop=0;' onload='allyes1()' >");
document.write("<a HREF='bbs/index.asp'>");
//document.write("<dd><img SRC='" + pic + "' border='0' alt='" + alt + "'></a></dd>");
document.write("<dd><img SRC='" + pic + "' border='0' alt='" + alt + "'></a></dd>");
document.write("</layer>");
}
var pic="images/dragon.swf";
var alt="查看奖品情况";
var gourl="bbs/index.asp"; //点击后连接的地址
var speed=100;//速度调整,越小越快
var light=0;//如果是1的话就自动改变透明度,否则不改变透明度
var stop=0;
var num=0;
var brOK=false;
var mie=false;
var aver=parseInt(navigator.appVersion.substring(0,1));
var aname=navigator.appName;
var vmin=2;
var vmax=5;
var vr=2;
var timer1;
var allyes;
var ns = (document.layers) ? 1 : 0;
var ie = (document.all) ? 1 : 0;
//var ie=0;
//var ns=1;
var bFade=false;
var photoname=1;
var counts=0;
function checkbrOK(){
if(aname.indexOf("Internet Explorer")!=-1){
if(aver>=4) brOK=navigator.javaEnabled();
mie=true;
}
if(aname.indexOf("Netscape")!=-1){
if(aver>=4) brOK=navigator.javaEnabled();
}
}
function Chip(chipname,width,height){
this.named=chipname;
this.vx=vmin+vmax*Math.random();
this.vy=vmin+vmax*Math.random();
this.w=width;
this.h=height;
this.xx=10;
this.yy=10;
this.timer1=null;
}
function movechip(chipname){
if (stop!=1){
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;
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.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);
}
//var temp=parseInt(chip.yy%4);
// if(temp!=0)
// document.all("oimg2").src=temp+".gif";
chip.timer1=setTimeout("movechip('"+chip.named+"')",speed);
}
}
else {chip.timer1=setTimeout("movechip('"+chip.named+"')",speed);}
if (ie){
if (light==1){
var nOpacity=oImg.filters.alpha.opacity;
if (nOpacity>=100) bFade=true;
if (nOpacity<=0) bFade=false;
if (bFade) oImg.filters.alpha.opacity--;
if (!bFade) oImg.filters.alpha.opacity++;
}
}
}
function stopme(chipname){
if(brOK){
eval("chip="+chipname);
if(chip.timer1!=null){
clearTimeout(chip.timer1)
}
}
}
function allyes1(){
if (num==0){
checkbrOK();
allyes=new Chip("allyes",100,80);
if(brOK) {
movechip("allyes");
}
}
num++;
}
if (ie){
document.write("<div ID='allyes' STYLE='position:absolute;left: 10px; top: 10px;'>");
document.write("<a HREF='"+gourl+"' target=_blank>");
if (light==1){
document.write("<dd><img SRC='" + pic + "' onload='allyes1()' ID='oImg' border='0' alt='" + alt + "' style='position:absolute;left:200px;top:200px;filter:alpha(opacity=50);' onMouseOver='stop=1;' onMouseOut='stop=0;'></a></dd>");
}
else if (light==0){
//document.write("<dd><div ><img id='oimg2' SRC='" + pic + "' border='0' alt='" + alt + "' onload='allyes1()' onMouseOver='stop=1;' onMouseOut='stop=0;'></div></a></dd>");
document.write("<dd><div id='myphoto' style='position: absolute;width:90;top:20;left:2;visibility: visible;z-index: 1' onMouseOver='stop=1;' onMouseOut='stop=0;'><EMBED src='"+pic+"' quality=high WIDTH=160px HEIGHT=160px TYPE='application/x-shockwave-flash' id=dragon wmode='transparent'></EMBED></div></dd></a>");
}
document.write("</div>");
}
else if (ns){
document.write("<layer name='allyes' visibility='1' onMouseOver='stop=1;' onMouseOut='stop=0;' onload='allyes1()' >");
document.write("<a HREF='bbs/index.asp'>");
//document.write("<dd><img SRC='" + pic + "' border='0' alt='" + alt + "'></a></dd>");
document.write("<dd><img SRC='" + pic + "' border='0' alt='" + alt + "'></a></dd>");
document.write("</layer>");
}
2、调用页面
只要在body加载的时候调用该函数就可以了。
<body onload="allyes1()">