飘浮广告显示脚本类(VBS,JS双版)

在写一个项目时要用到广告模块,为了不想用ASP生成脚本代码时较麻烦,于是产生了写脚本类的念头,即是用一个类模块的脚本代码去控制所有在同一页面显示的漂浮广告。但在写的过程中发现JS脚本竟然在setTimeout里不能使用类的方法。奇怪,是不是我弄错了还是JS脚本就不能这样??但VBS脚本就可以!我晕……

不说了,贴代码:
[VBS脚本代码]

以下是代码片段:
'/****************漂浮广告显示类****************************************************
'/*作者:死在水中的鱼
'/*脚本语言:VBS
'/*用法:
'/*SetAdver1=NewAdverClass
'/*Adver1.ObjName="Adver1"'设置当前的对象名[本身对象变量名]
'/*Adver1.ImgType=1'图片的类型0=普通图片1=Flash动画
'/*Adver1.ImageWidth=299'图片的宽度
'/*Adver1.ImageHeight=87'图片的高度
'/*####以下方法显示广告图片(Flash)对象.PrintHtml"图片地址","链接地址","提示信息"
'/*Adver1.PrintHtml"http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/'target="_blank">http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/","这是什么"
'/***********************************************************************************
ClassAdverClass
PublicDivID
PublicObjName
PrivateObjDiv
PublicDelay'改变数
PublicImgType
PrivateiStep
PrivateiTop,iLeft,Width,Height
PrivateTopFlag,LeftFlag
'广告图片的大小
PublicImageWidth,ImageHeight
PrivateSubClass_Initialize
Randomize
DivID=Int(Rnd(Time)*9999+1)
Delay=80
Height=document.body.clientHeight
Width=document.body.clientWidth
iTop=0
iLeft=0
TopFlag=False:LeftFlag=False
iStep=3
ImgType=0'0是图片1是FLASH文件
ImageWidth=0
ImageHeight=0
EndSub
PrivateSubClass_Terminate
EndSub
PublicSubScrollImg()
DimoffHeight,offWidth,iRnd
offWidth=ObjDiv.offsetWidth
offHeight=ObjDiv.offsetHeight
ObjDiv.style.left=iLeft+document.body.scrollLeft
ObjDiv.style.top=iTop+document.body.scrollTop
iRnd=Int(Rnd(Time)*99+1)
IfiRnd>97ThenTopFlag=NotTopFlag
iRnd=Int(Rnd(Time)*9+1)
IfiRnd>98ThenLeftFlag=NotLeftFlag
IfTopFlagThen
iTop=iTop+iStep*Rnd(Time)
Else
iTop=iTop-iStep*Rnd(Time)
EndIf
IfLeftFlagThen
iLeft=iLeft+iStep*Rnd(Time)
Else
iLeft=iLeft-iStep*Rnd(Time)
EndIf
IfiTop<0Then
iTop=0
TopFlag=True
ElseIfiTop>Height-offHeightThen
iTop=Height-offHeight
TopFlag=False
EndIf
IfiLeft<0Then
iLeft=0
LeftFlag=True
ElseIfiLeft>Width-offWidthThen
iLeft=Width-offWidth
LeftFlag=False
EndIf
EndSub
PrivateSubStart()
setIntervalObjName&".ScrollImg()",Delay
EndSub
PublicSubPrintHtml(ByValImgSrc,ByValadHref,ByValadTitle)
IfImgType=0Then
CallPrintImageHtml(ImgSrc,adHref,adTitle)
Else
CallPrintFlashHtml(ImgSrc,adHref,adTitle)
EndIf
Execute"SetObjDiv=document.all.img"&DivID
iLeft=Int(Rnd(Time)*(Width-100)+1)
iTop=Int(Rnd(Time)*(Height-100)+1)
ObjDiv.style.top=iTop
ObjDiv.style.left=iLeft
CallStart()
EndSub
PrivateSubPrintImageHtml(ByValImgSrc,ByValadHref,ByValadTitle)
IfImageWidth=0OrNotIsNumeric(ImageWidth)Then
ImageWidth=""
Else
ImageWidth="width='"&ImageWidth&"'"
EndIf
IfImageHeight=0OrNotIsNumeric(ImageHeight)Then
ImageHeight=""
Else
ImageHeight="height='"&ImageHeight&"'"
EndIf
document.write"<divid=""img"&DivID&"""style=""position:absolute;"">"
document.write"<ahref="""&adHref&"""target=""_blank""title='"&adTitle&"'>"
document.write"<imgsrc="""&ImgSrc&"""alt="""&adTitle&"""border=""0"""&ImageWidth&ImageHeight&"></a></div>"
EndSub
PrivateSubPrintFlashHtml(ByValImgSrc,ByValadHref,ByValadTitle)
IfImageWidth=0OrNotIsNumeric(ImageWidth)Then
ImageWidth=80
EndIf
ImageWidth="width='"&ImageWidth&"'"
IfImageHeight=0OrNotIsNumeric(ImageHeight)Then
ImageHeight=80
EndIf
ImageHeight="height='"&ImageHeight&"'"
document.write"<divid=""img"&DivID&"""style=""position:absolute;"">"
document.write"<ahref="""&adHref&"""target=""_blank""title='"&adTitle&"'>"
document.write"<objectcodebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'"&ImageWidth&ImageHeight&"align='middle'>"
document.write"<paramname='movie'value='"&ImgSrc&"'>"
document.write"<paramname='quality'value='high'>"
document.write"<embedsrc='"&ImgSrc&"'"&ImageWidth&ImageHeight&"quality='high'pluginspage='http://www.macromedia.com/go/getflashplayer&#39;type='application/x-shockwave-flash'></embed></object>"
document.write"</a></div>"
EndSub
EndClass

[JS脚本]

以下是代码片段:
file://*****************漂浮广告显示类****************************************************
file://*作者:死在水中的鱼
file://*脚本语言:Javascript
file://*用法:
file://*varadver=newadverClass
file://*adver.setObjName("adver");file://这句不能setTime所以没有用
file://*adver.setDivID(1);file://这句可以不用
file://*adver.setImageType(1);file://设为0时或不写这句时则是普通图片,1则为Flash动画文件
file://*adver.setImagePX(299,87);file://设置图片的宽度与高度
file://*adver.showAdver("http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/'target="_blank">http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/","这是什么");
file://*varadver1=newadverClass();
file://*adver1.showAdver("img.gif","http://www.chinese.bj.cn/","这是什么");
file://*setTimer();
file://*file://因为类里不能写setTime所以只好拿出来写-_-##(VBS脚本竟然可以。我倒)
file://*functionsetTimer(){
file://*adver.floatScroll();
file://*adver1.floatScroll();
file://*setTimeout("setTimer()",80);
file://*}
file://***********************************************************************************
functionadverClass(){
varobjName="";
varimageType=0;
varimageWidth=imageHeight=0;
variTop=iLeft=0;
vartopFlag=leftFlag=false;
vardivID=0,objDiv=null;
varoffWidth=offHeight=0;
varwidth=document.body.clientWidth;
varheight=document.body.clientHeight;
vardelay=30;file://时间的延迟值
vartopStep=2,leftStep=3;file://一步跨多少
varinter;
file://此处是当外部不设置divID的值能够确保层的ID是唯一
divID=Math.round(Math.random()*100000)
file://广告的类型0=普通图片1=Flash广告图片
this.setImageType=function(sType){
if(sType!=1&&sType!=0){sType=0;}
imageType=sType;
}
file://外部调用的变量名
this.setObjName=function(sName){objName=sName;}
file://广告图片的高度与宽度
this.setImagePX=function(iWidth,iHeight){
if(!isNaN(iWidth)){
imageWidth=iWidth;
}else{
imageWidth=0;
}
if(!isNaN(iHeight)){
imageHeight=iHeight;
}else{
imageHeight=0;
}
}
file://设置广告所在层的ID值
this.setDivID=function(iDiv){divID=iDiv;}

file://主函数,显示广告代码
this.showAdver=function(adImgSrc,adHref,adTitle){
if(imageType==0){
showImageHtml(adImgSrc,adHref,adTitle);
}else{
showFlashHtml(adImgSrc,adHref,adTitle);
}
eval("objDiv=document.all.img"+divID+";");
file://取得图片的宽度
offWidth=objDiv.offsetWidth;
offHeight=objDiv.offsetHeight;
file://随机显示广告的开始位置
iLeft=Math.round(Math.random()*(width-offWidth));
iTop=Math.round(Math.random()*(height-offHeight));
objDiv.style.pixelLeft=iLeft;
objDiv.style.pixelTop=iTop;
file://定时开始
file://startTimer();
}
file://主函数,漂浮游动显示广告
this.floatScroll=function(){
variRnd;
iRnd=Math.round(Math.random()*100);file://此值是为了能使多图显示时产生不同的轨迹
if(objDiv==null)return;
objDiv.style.pixelLeft=iLeft+document.body.scrollLeft;
objDiv.style.pixelTop=iTop+document.body.scrollTop;
if(iRnd>98){leftFlag=!leftFlag;}
iRnd=Math.round(Math.random()*100);
if(iRnd>99){topFlag=!topFlag;}
if(leftFlag){
iLeft=iLeft+leftStep*Math.random();
}else{
iLeft=iLeft-leftStep*Math.random();
}
if(topFlag){
iTop=iTop+topStep*Math.random();
}else{
iTop=iTop-topStep*Math.random();
}
if(iLeft<0){
iLeft=0;
leftFlag=true;
}
elseif(iLeft>width-offWidth){
iLeft=width-offWidth;
leftFlag=false;
}
if(iTop<0){
iTop=0;
topFlag=true;
}
elseif(iTop>height-offHeight){
iTop=height-offHeight;
topFlag=false;
}
}
file://定时移动广告的图片
functionstartTimer(){
if(objName=="")return;
file://alert(objName+".floatScroll();");
file://inter=setInterval(objName+".floatScroll()",delay);
}
file://显示图片的HTML代码
functionshowImageHtml(adImgSrc,adHref,adTitle){
varsWidth,sHeight;
if(imageWidth<5){
sWidth="";
}else{
sWidth="width='"+imageWidth+"'";
}
if(imageHeight<5){
sHeight="";
}else{
sHeight="height='"+imageHeight+"'";
}
document.write("<divid='img"+divID+"'style='position:absolute;'>");
document.write("<ahref='"+adHref+"'target='_blank'title='"+adTitle+"'>");
document.write("<imgsrc='"+adImgSrc+"'border='0'"+sWidth+sHeight+">");
document.write("</a></div>");
}
file://显示Flash文件的HTML代码
functionshowFlashHtml(adImgSrc,adHref,adTitle){
varsWidth,sHeight;
if(imageWidth<5){
sWidth="width='80'";
}else{
sWidth="width='"+imageWidth+"'";
}
if(imageHeight<5){
sHeight="height='80'";
}else{
sHeight="height='"+imageHeight+"'";
}
document.write("<divid='img"+divID+"'style='position:absolute;'>");
document.write("<ahref='"+adHref+"'target='_blank'title='"+adTitle+"'>");
document.write("<objectcodebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'"+sWidth+sHeight+"align='middle'>");
document.write("<paramname='movie'value='"+adImgSrc+"'>");
document.write("<paramname='quality'value='high'>");
document.write("<embedsrc='"+adImgSrc+"'"+sWidth+sHeight+"quality='high'pluginspage='http://www.macromedia.com/go/getflashplayer&#39;type='application/x-shockwave-flash'></embed></object>");
document.write("</a></div>");
}
}

示例代码(包括两种脚本的使用):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 漂浮广告显示脚本类演示 </TITLE>
</HEAD>

<BODY>
<!--VBS脚本版的广告显示类示例程序-->
<script LANGUAGE="vbscript" src="Advervbs.vbs"></script>
<script LANGUAGE="vbscript">
Dim Adver,Adver1
Set Adver=New AdverClass
Adver.ObjName="Adver"
Adver.DivID=20
Adver.PrintHtml "img.gif","http://www.chinese.bj.cn/","这是什么"
Set Adver1=New AdverClass
Adver1.ObjName="Adver1"
Adver1.ImgType=1
Adver1.ImageWidth=299
Adver1.ImageHeight=87
Adver1.PrintHtml "http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/' target="_blank" >http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/","这是什么"
</script>
<!--JS脚本版的广告显示类示例程序-->
<script LANGUAGE="Javascript" src="AdverClass.js"></script>
<script LANGUAGE="Javascript">
var adver=new adverClass();
adver.setObjName("adver");
adver.setDivID(1);
adver.setImageType(1);
adver.setImagePX(299,87);
adver.showAdver("http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/' target="_blank" >http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/","这是什么");
var adver1=new adverClass();
adver1.showAdver("img.gif","http://www.chinese.bj.cn/","这是什么");
setTimer();
function setTimer(){
adver.floatScroll();
adver1.floatScroll();
setTimeout("setTimer()",80);
}
</script>
</BODY>
</HTML>

你可能感兴趣的:(JavaScript,qq,脚本,Flash,VBScript)