如果你的主页中需要随机显示图片或者链接banner,你该怎么实现呢?
下面是一个随机显示五条Banner的代码,你可以自己定义的。
<script language=javascript>
var m=5; //共几个Banner随机显示
var n=Math.floor(Math.random()*m+1)
switch(n)
{
case 1:
document.write('Banner1的HTML');
break;
case 2:
document.write('Banner2的HTML');
break;
case 3:
document.write('Banner3的HTML');
break;
case 4:
document.write('Banner4的HTML');
break;
case 5:
document.write('Banner5的HTML');
break;
}
</script></JS>
如果一些如广告类的图片上需要加入相应的连接呢?那么代码仍然大同小异:
<JS>
<SCRIPT LANGUAGE="javascript">
<!-- Begin
var how_many_ads = 3;
var now = new Date()
var sec = now.getSeconds()
var ad = sec % how_many_ads;
ad +=1;
if (ad==1) {
txt="banner01";
url="http://www.xxy8.com/banner01";
alt="banner01";
banner="image/banner01.gif";
width="468";
height="60";
}
if (ad==2) {
txt="banner02";
url="http://www.xxy8.com/banner02";
alt="banner02";
banner="image/banner02.gif";
width="468";
height="60";
}
if (ad==3) {
txt="banner03";
url="http://www.xxy8.com/banner03";
alt="banner03";
banner="image/banner03.gif";
width="468";
height="60";
}
document.write('<center>');
document.write('<a href="' + url + '" target="_top">');
document.write('<img src="' + banner + '" width=')
document.write(width + ' height=' + height + ' ');
document.write('alt="' + alt + '" border=0><br>');
document.write('<small>' + txt + '</small></a>');
document.write('</center>');
// End -->
</SCRIPT>
另外,有
如果你的主页中需要随机显示图片或者链接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.it365cn.com/images/banner/banner.gif"
// Banner图片路径
b[0].href ="http://www.it365cn.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.it365cn.com/images/banner/eschool.gif"
b[2].href ="http://favorites.myrice.com"
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.it365cn.com/images/banner/wxepower.gif"
b[3].href ="http://www.wxepower.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 m=5; //共几个Banner随机显示
var n=Math.floor(Math.random()*m+1)
switch(n)
{
case 1:
document.write('Banner1的HTML');
break;
case 2:
document.write('Banner2的HTML');
break;
case 3:
document.write('Banner3的HTML');
break;
case 4:
document.write('Banner4的HTML');
break;
case 5:
document.write('Banner5的HTML');
break;
}
</script></JS>
如果一些如广告类的图片上需要加入相应的连接呢?那么代码仍然大同小异:
<JS>
<SCRIPT LANGUAGE="javascript">
<!-- Begin
var how_many_ads = 3;
var now = new Date()
var sec = now.getSeconds()
var ad = sec % how_many_ads;
ad +=1;
if (ad==1) {
txt="banner01";
url="http://www.xxy8.com/banner01";
alt="banner01";
banner="image/banner01.gif";
width="468";
height="60";
}
if (ad==2) {
txt="banner02";
url="http://www.xxy8.com/banner02";
alt="banner02";
banner="image/banner02.gif";
width="468";
height="60";
}
if (ad==3) {
txt="banner03";
url="http://www.xxy8.com/banner03";
alt="banner03";
banner="image/banner03.gif";
width="468";
height="60";
}
document.write('<center>');
document.write('<a href="' + url + '" target="_top">');
document.write('<img src="' + banner + '" width=')
document.write(width + ' height=' + height + ' ');
document.write('alt="' + alt + '" border=0><br>');
document.write('<small>' + txt + '</small></a>');
document.write('</center>');
// End -->
</SCRIPT>
另外,有
如果你的主页中需要随机显示图片或者链接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.it365cn.com/images/banner/banner.gif"
// Banner图片路径
b[0].href ="http://www.it365cn.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.it365cn.com/images/banner/eschool.gif"
b[2].href ="http://favorites.myrice.com"
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.it365cn.com/images/banner/wxepower.gif"
b[3].href ="http://www.wxepower.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的数量。