图片或BANNER的随机显示
类别: JavaScript教程
如果你的主页中需要随机显示图片或者链接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://www.etoow.com/images/468_60.gif"
// Banner图片路径
b[0].href ="http://www.etoow.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://www.etoow.com/ads/sucai114.gif"
b[2].href ="http://www.etoow.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://www.etoow.com/ads/banner.gif"
b[3].href ="http://bbs.etoow.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的代码,你可以自己定义的。
<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://www.etoow.com/images/468_60.gif"
// Banner图片路径
b[0].href ="http://www.etoow.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://www.etoow.com/ads/sucai114.gif"
b[2].href ="http://www.etoow.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://www.etoow.com/ads/banner.gif"
b[3].href ="http://bbs.etoow.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的数量。
- 上一篇: 页面两侧对联广告代码效果
- 下一篇: 使用JAVASCRIPT的日期函数
-= 资 源 教 程 =-
文 章 搜 索