|
如果你的主页中需要随机显示图片或者链接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>
--------------------------------------------------------------------------------
如果一些如广告类的图片上需要加入相应的连接呢?那么代码仍然大同小异:
代码:-------------------------------------------------------------------------------- <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://siyizhu.com";
alt="banner01";
banner="http://siyizhu.com/images/banner/webbanner1.jpg";
width="778";
height="160";
}
if (ad==2) {
txt="banner02";
url="http://siyizhu.com";
alt="banner02";
banner="http://siyizhu.com/images/banner/webbanner2.jpg";
width="778";
height="160";
}
if (ad==3) {
txt="banner03";
url="http://siyizhu.com";
alt="banner03";
banner="http://siyizhu.com/images/banner/webbanner3.jpg";
width="778";
height="160";
}
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> |
|