图片或Banner的随机显示

时间:2010-04-20 17:40:52  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:um =12var b=new Array();function init_item(){this.width = ''this.height = ''this.src = ''this.href = ''this.border = ''this.alt = ''this.under = ''this.grade=0this.target=''}function createarray(ba, itemnum) {for(var i=0; i<itemnum; i++) { ba[i] = new init_item...
  正文:如果你的主页中需要随机显示图片或者链接banner,你该怎么实现呢?我们介绍使用JavaScript来实现这样的效果!

下面是一个随机显示五条Banner的代码,你可以自己定义的。
<script language=javascript> 
<!--
var b_num =12
var b=new Array();

function init_item(){
this.width = ''
this.height = ''
this.src = ''
this.href = ''
this.border = ''
this.alt = ''
this.under = ''
this.grade=0
this.target=''
}

function createarray(ba, itemnum) {
for(var i=0; i<itemnum; i++) { ba[i] = new init_item() }
}

function requestbanner(ba){
sum=0;
for(i=0;i<ba.length;i++)sum+=ba[i].grade
var slump = Math.random();
var talet = Math.round(slump*(sum-1))+1;
i=0;
sum=0;
while(i<ba.length && sum<=talet){sum+=ba[i].grade;i++;}
b=ba[i-1];
visa = '';
if (b.target!=''){
visa += '<a href="'+b.href+'" target='+b.target+'>';}
else{
visa += '<a href="'+b.href+'">';
}
visa += '<img src="'+b.src+'" height='+b.height;
visa += ' width='+b.width+' border='+b.border+' alt='+b.alt+'>';
visa += '</a>';
document.write(visa);
}

createarray(b,b_num)

b
b[0].src ="http://webdesign.chinaitlab.com/UploadFiles_8014/200604/20060418095601873.gif"
// Banner图片路径
b[0].href ="http://www.webjx.com"
// Banner链接地址
b[0].target="_blank"
b[0].width = "468"
b[0].height = "60"
b[0].border = "0"
b[0].alt = "::::网页教学网::::"
// Banner文字提示
b[0].under = ''
b[0].grade = 20

b
b[2].src ="http://webdesign.chinaitlab.com/UploadFiles_8014/200604/20060418095601291.gif"
b[2].href ="http://www.webjx.net"
b[2].target="_blank"
b[2].width = "468"
b[2].height = "60"
b[2].border = "0"
b[2].alt = "::::备用域名::::"
b[2].under = ''
b[2].grade = 20

b
b[3].src ="http://webdesign.chinaitlab.com/UploadFiles_8014/200604/20060418095601541.gif"
b[3].href ="http://bbs.webjx.com"
b[3].target="_blank"
b[3].width = "468"
b[3].height = "60"
b[3].border = "0"
b[3].alt = "::::技术论坛::::"
b[3].under = ''
b[3].grade = 20

requestbanner(b);
// -->
</Script>
将这段JS代码插入到<body>相应位置。
用户可修改相关代码,或者增加、减少Banner的数量。 "图片或Banner的随机显示"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/JavaScript/13720.html


关键字:

关于《图片或Banner的随机显示》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td