具体代码如下:
moveobj.js:
var vmin=2;var vmax=5;var vr=2;var timer1;function iecompattest(){return (document.compatmode && document.compatmode!=backcompat)? document.documentelement : document.body}function chip(chipname,width,height){ this.named=chipname; this.vx=vmin+vmax*math.random(); this.vy=vmin+vmax*math.random(); this.w=width+20; this.h=height; this.xx=0; this.yy=0; this.timer1=null;}function movechip(chipname){if (document.getelementbyid){eval(chip=+chipname); if (window.innerwidth || window.opera){ pagex=window.pagexoffset; pagew=window.innerwidth-40; pagey=window.pageyoffset; pageh=window.innerheight-20; } else if (document.body){ pagex=iecompattest().scrollleft; pagew=iecompattest().offsetwidth-40; pagey=iecompattest().scrolltop; pageh=iecompattest().offsetheight-20; } 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.vy=(vmax+vmin)*2-chip.vy; if(chip.vy<(-vmax-vmin)) chip.vy=(-vmax-vmin)*2-chip.vy; if(chip.xx=pagex+pagew-chip.w){ chip.xx=pagex+pagew-chip.w; chip.vx=-vmin-vmax*math.random(); } if(chip.yy=pagey+pageh-chip.h) {chip.yy=pagey+pageh-chip.h; chip.vy=-vmin-vmax*math.random(); }document.getelementbyid(chip.named).style.left=chip.xx+px;document.getelementbyid(chip.named).style.top=chip.yy+px; chip.timer1=settimeout(movechip('+chip.named+'),100); }}
运行效果图:
此特效包含的文件:
希望本文所述对大家的javascript程序设计有所帮助。
