在写一个项目时要用到广告模块,为了不想用asp生成脚本代码时较麻烦,于是产生了写脚本类的念头,即是用一个类模块的脚本代码去控制所有在同一页面显示的漂浮广告。但在写的过程中发现js脚本竟然在settimeout里不能使用类的方法。奇怪,是不是我弄错了还是js脚本就不能这样??但vbs脚本就可以!我晕……
不说了,贴代码:
[vbs脚本代码]
以下是代码片段:
/****************漂浮广告显示类****************************************************
/* 作者:死在水中的鱼
/* 脚本语言:vbs
/* 用法:
/* set adver1=new adverclass
/* 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/","这是什么"
/***********************************************************************************
class adverclass
public divid
public objname
private objdiv
public delay 改变数
public imgtype
private istep
private itop,ileft,width,height
private topflag,leftflag
广告图片的大小
public imagewidth,imageheight
private sub class_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
end sub
private sub class_terminate
end sub
public sub scrollimg()
dim offheight,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)
if irnd>97 then topflag=not topflag
irnd=int(rnd(time)*9+1)
if irnd>98 then leftflag=not leftflag
if topflag then
itop=itop+istep*rnd(time)
else
itop=itop-istep*rnd(time)
end if
if leftflag then
ileft=ileft+istep*rnd(time)
else
ileft=ileft-istep*rnd(time)
end if
if itop<0 then
itop=0
topflag=true
elseif itop>height-offheight then
itop=height-offheight
topflag=false
end if
if ileft<0 then
ileft=0
leftflag=true
elseif ileft>width-offwidth then
ileft=width-offwidth
leftflag=false
end if
end sub
private sub start()
setinterval objname&".scrollimg()", delay
end sub
public sub printhtml(byval imgsrc,byval adhref,byval adtitle)
if imgtype=0 then
call printimagehtml(imgsrc,adhref,adtitle)
else
call printflashhtml(imgsrc,adhref,adtitle)
end if
execute "set objdiv=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
call start()
end sub
private sub printimagehtml(byval imgsrc,byval adhref,byval adtitle)
if imagewidth=0 or not isnumeric(imagewidth) then
imagewidth=""
else
imagewidth=" width="&imagewidth&""
end if
if imageheight=0 or not isnumeric(imageheight) then
imageheight=""
else
imageheight=" height="&imageheight&""
end if
document.write "<div id=""img"&divid&""" style=""position:absolute;"">"
document.write "<a href="""&adhref&""" target=""_blank"" title="&adtitle&">"
document.write "<img src="""&imgsrc&""" alt="""&adtitle&""" border=""0"""&imagewidth&imageheight&"></a></div>"
end sub
private sub printflashhtml(byval imgsrc,byval adhref,byval adtitle)
if imagewidth=0 or not isnumeric(imagewidth) then
imagewidth=80
end if
imagewidth=" width="&imagewidth&""
if imageheight=0 or not isnumeric(imageheight) then
imageheight=80
end if
imageheight=" height="&imageheight&""
document.write "<div id=""img"&divid&""" style=""position:absolute;"">"
document.write "<a href="""&adhref&""" target=""_blank"" title="&adtitle&">"
document.write "<object codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"&imagewidth&imageheight&" align=middle>"
document.write "<param name=movie value="&imgsrc&">"
document.write "<param name=quality value=high>"
document.write "<embed src="&imgsrc&""&imagewidth&imageheight&" quality=high pluginspage=http://www.macromedia.com/go/getflashplayer' type=application/x-shockwave-flash></embed></object>"
document.write "</a></div>"
end sub
end class
[js脚本]
以下是代码片段:
file://*****************漂浮广告显示类****************************************************
file://* 作者:死在水中的鱼
file://* 脚本语言:javascript
file://* 用法:
file://* var adver=new adverclass
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://* var adver1=new adverclass();
file://* adver1.showadver("img.gif","http://www.chinese.bj.cn/","这是什么");
file://* settimer();
file://* file://因为类里不能写settime所以只好拿出来写-_-##(vbs脚本竟然可以。我倒)
file://* function settimer(){
file://* adver.floatscroll();
file://* adver1.floatscroll();
file://* settimeout("settimer()",80);
file://* }
file://***********************************************************************************
function adverclass(){
var objname="";
var imagetype=0;
var imagewidth=imageheight=0;
var itop=ileft=0;
var topflag=leftflag=false;
var divid=0,objdiv=null;
var offwidth=offheight=0;
var width=document.body.clientwidth;
var height=document.body.clientheight;
var delay=30; file://时间的延迟值
var topstep=2,leftstep=3; file://一步跨多少
var inter;
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(){
var irnd;
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;
}
else if(ileft>width-offwidth){
ileft=width-offwidth;
leftflag=false;
}
if(itop<0){
itop=0;
topflag=true;
}
else if(itop>height-offheight){
itop=height-offheight;
topflag=false;
}
}
file://定时移动广告的图片
function starttimer(){
if(objname=="")return;
file://alert(objname+".floatscroll();");
file://inter=setinterval(objname+".floatscroll()",delay);
}
file://显示图片的html代码
function showimagehtml(adimgsrc,adhref,adtitle){
var swidth,sheight;
if(imagewidth<5){
swidth="";
}else{
swidth=" width="+imagewidth+"";
}
if(imageheight<5){
sheight="";
}else{
sheight=" height="+imageheight+"";
}
document.write("<div id=img"+divid+" style=position:absolute;>");
document.write("<a href="+adhref+" target=_blank title="+adtitle+">");
document.write("<img src="+adimgsrc+" border=0"+swidth+sheight+">");
document.write("</a></div>");
}
file://显示flash文件的html代码
function showflashhtml(adimgsrc,adhref,adtitle){
var swidth,sheight;
if(imagewidth<5){
swidth=" width=80";
}else{
swidth=" width="+imagewidth+"";
}
if(imageheight<5){
sheight=" height=80";
}else{
sheight=" height="+imageheight+"";
}
document.write("<div id=img"+divid+" style=position:absolute;>");
document.write("<a href="+adhref+" target=_blank title="+adtitle+">");
document.write("<object codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"+swidth+sheight+" align=middle>");
document.write("<param name=movie value="+adimgsrc+">");
document.write("<param name=quality value=high>");
document.write("<embed src="+adimgsrc+""+swidth+sheight+" quality=high pluginspage=http://www.macromedia.com/go/getflashplayer' 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>
