如果你的主页中需要随机显示图片或者链接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的数量。