以下的js腳本(script01.js)演示如何將循環(huán)廣告條轉(zhuǎn)換為真正的可點擊的廣告條
[javascript]
window.onload = initBannerLink;
var thisAd = 0;
function initBannerLink() {
if (document.getElementById("adBanner").parentNode.tagName == "A") {
document.getElementById("adBanner").parentNode.onclick = newLocation;
}
//檢查adBanner對象是否包圍在鏈接標(biāo)簽中,如果是這樣,那么當(dāng)點擊鏈接時,將調(diào)用newLocation()函數(shù).
rotate();
}
function newLocation() {
var adURL = new Array("negrino.com","sun.com","microsoft.com");
document.location.href = "http://www." + adURL[thisAd];//將當(dāng)前文檔窗口設(shè)置為文本字符串<a href="http://www.php1.cn/">http://www">http://www.加上adURL的</a>的值 return false;//告訴瀏覽器不要再加載這個href,否則會加載URL兩次
}
function rotate() {
var adImages = new Array("images/banner1.gif","images/banner2.gif","images/banner3.gif");
thisAd++;
if (thisAd == adImages.length) {
thisAd = 0;
}
document.getElementById("adBanner").src = adImages[thisAd];
setTimeout(rotate, 3 * 1000);
}
注意:adURL數(shù)組中的成員數(shù)量必須與adImages數(shù)組相同,這個腳本才能正常工作
建一個images文件夾,然后存入banner1.gif,banner2.gif,banner3.gif三張gif格式圖片
(二)隨機開始循環(huán)顯示圖像
如果有很多圖片需要顯示,可能不希望在每次加載頁面時都從同樣的圖像開始顯示,下面HTML和js組合可以實現(xiàn)隨機開始
[html]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Rotating Random Banner</title>
<script type="text/javascript" src="script02.js"></script>
</head>
<body bgcolor="#FFFFFF">
<div align="center">
<img src="images/spacer.gif" width="400" height="75" id="adBanner" alt="Ad Banner" />
</div>
</body>
</html>
以下js腳本(script02.js)可以從一個隨機圖像開始顯示圖像
[javascript]
window.onload = choosePic;
var adImages = new Array("images/reading1.gif","images/reading2.gif","images/reading3.gif");
var thisAd = 0;
function choosePic() {
thisAd = Math.floor((Math.random() * adImages.length));
document.getElementById("adBanner").src = adImages[thisAd];
rotate();
}
function rotate() {
thisAd++;
if (thisAd == adImages.length) {
thisAd = 0;
}
document.getElementById("adBanner").src = adImages[thisAd];
setTimeout(rotate, 3 * 1000);
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com