JavaScript筆記之處理映像

來源:互聯網
上載者:User

 JavaScript最常見也最顯著的用途之一是在網頁上添加動畫,從而在視覺上更具有吸引力。其中有翻轉器效果,廣告條應用。記錄兩個實用的樣本:
     (一)在迴圈廣告條中添加連結
     這樣可以讓訪問者通過點選連結進入與廣告相關的網站。
廣告條所需的HTML
[html]
        <!-- 在迴圈廣告中添加連結 --> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Rotating Banner with Links</title> 
    <script type="text/javascript" src="script01.js"></script> 
</head> 
<body bgcolor="#FFFFFF"> 
    <div align="center"> 
        <a href="linkPage.html"><img src="images/banner1.gif" width="400" height="75" id="adBanner" border="0" alt="ad banner" /></a> 
    </div> 
</body> 
</html> 

以下的js指令碼(script01.js)示範如何將迴圈廣告條轉換為真正的可點擊的廣告條
[javascript]
window.onload = initBannerLink; 
 
var thisAd = 0; 
 
function initBannerLink() { 
    if (document.getElementById("adBanner").parentNode.tagName == "A") { 
        document.getElementById("adBanner").parentNode.onclick = newLocation; 
    } 
//檢查adBanner對象是否包圍在連結標籤中,如果是這樣,那麼當點選連結時,將調用newLocation()函數.  
    rotate(); 
} 
 
function newLocation() { 
    var adURL = new Array("negrino.com","sun.com","microsoft.com"); 
    document.location.href = "http://www." + adURL[thisAd];//將當前文件視窗設定為文本字串<a href="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數組中的成員數量必須與adImages數組相同,這個指令碼才能正常工作
          建一個images檔案夾,然後存入banner1.gif,banner2.gif,banner3.gif三張gif格式圖片
 
(二)隨機開始迴圈顯示映像
如果有很多圖片需要顯示,可能不希望在每次載入頁面時都從同樣的映像開始顯示,下面HTML和js組合可以實現隨機開始
[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); 
} 
同樣建立一個images檔案夾,加入reading1等三張gif格式圖片。(源《JavaScript基礎教程》) 


摘自  Yanghai 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.