我有以下代碼在論壇上旋轉橫幅,爲了在此發佈代碼,我擴展了網址,因此每個人都可以看到它是如何工作的,而不僅僅是簡短的網址。Javascript隨機號碼顯示
正如您可以在每次刷新頁面時看到的,我的腳本從第一張圖像開始。
我期望讓腳本以0-6之間的隨機數開始(或者我設置的任何數字),以便每次頁面加載時不會總是以相同的橫幅出現。
我很高興定時輪換,不想改變這一點。
<script language="JavaScript1.2">
var howOften = 30; // number often in seconds to rotate
var current = 0; // start the counter at 0
var ns6 = document.getElementById&&!document.all; // detect netscape 6
// place your images, text, etc in the array elements here
var items = new Array();
items[0]="<a href=http://www.hornady.com/ ><img alt='image0 (9K)' src='http://wwww.bulletdrop.co.uk/ads/png2.png' width='60%' /></a>";
items[1]="<a href='http://www.blackfoxoptics.co.uk/'><img alt='image1 (9K)' src='http://wwww.bulletdrop.co.uk/ads/png1.png' width='60%' /></a>";
items[2]="<a href='http://epgroupuk.com/'><img alt='image2 (9K)' src='http://wwww.bulletdrop.co.uk/ads/png3.png' width='60%' /></a>";
items[3]="<a href='https://barrett.net/'><img alt='image3 (9K)' src='http://wwww.bulletdrop.co.uk/ads/png4.png' width='60%' /></a>";
items[4]="<a href='link.htm'><img alt='image4 (9K)' src='http://wwww.bulletdrop.co.uk/ads/png5.png' height='120' width='600' border='0'/></a>";
items[5]="<a href='link.htm'><img alt='image5 (18K)' src='/Images/image5.jpg' height='300' width='300' border='0' /></a>";
function rotater() {
document.getElementById("placeholder").innerHTML = items[current];
current = (current == items.length-1) ? 0 : current + 1;
setTimeout("rotater()", howOften*1000);
}
function rotater() {
if (document.layers) {
document.placeholderlayer.document.write(items[current]);
document.placeholderlayer.document.close();
}
if (ns6) document.getElementById("placeholderdiv").innerHTML = items[current]
if (document.all) placeholderdiv.innerHTML = items[current];
current = (current == items.length-1) ? 0 : current + 1; // increment or reset
setTimeout("rotater()", howOften*1000);
}
window.onload = rotater;
//-->
對不起不能獲取代碼,以顯示正確的。
我會再調用使用腳本:
<layer id="placeholderlayer" ></layer><div align="center"
id="placeholderdiv"></div>
希望有人能幫助您?
邁克
你看過[Math.random()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)嗎? – Ionut
爲什麼兩個旋轉方法? –