JavaScript圖片翻轉

來源:互聯網
上載者:User

標籤:loaded   mouse   模組   建立   else   翻轉   normal   eve   attach   

<script type="text/javascript">/** * 註冊函數f,當文檔載入問成時執行這個函數f * 如果檔案已經載入完成,儘快以非同步方式執行它 */function onLoad(f){if(onLoad.loaded)window.setTimeout(f,0);else if(window.addEventListener)window.addEventListener("load",f,false);else if(window.attachEvent)window.attachEvent("onload",f);}//給onLoad設定一個標誌,用來指示文檔是否載入完成onLoad.loaded = false;//註冊一個函數,當文檔載入完成時設定這個標誌onLoad(function(){onLoad.loaded = true;});/** * 要建立圖片翻轉效果,將此模組引入到HTML檔案中 * 然後再任意<img>元素上使用data-rollover屬性來指定翻轉圖片的URL即可 * 如下所示: * <img src="normal.png" data-rollover="rollover_image.png"> * 要注意的是,此模組依賴於 onLoad.js */onLoad(function(){//所有處理邏輯都在一個匿名函數中,不定義任何符號for(var i=0 ; i < document.images.length ; i++){var img = document.images[i];var rollover = img.getAttribute("data-rollover");if(!rollover)continue;//確保將翻轉的圖片緩衝起來(new Image()).src = rollover;//定義一個屬性來標識預設的圖片和URLimg.setAttribute("data-rollover",img.src);//註冊事件處理函數來建立翻轉效果img.onmouseover = function(){this.src = this.getAttribute("data-rollover");};img.onmouseout = function(){this.src = this.getAttribute("data-rollout");};}});</script>

  

JavaScript圖片翻轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.