2012-08-08 53 views
1

我試圖在用戶點擊一個鏈接時顯示一個加載圖片,該鏈接將在同一頁面顯示大圖。如何檢測瀏覽器是否正在加載

我想知道什麼是檢測圖像加載的最佳方式,而頁面已經加載(所以window.onload()不起作用)。

+0

Duplicate - http://stackoverflow.com/questions/476679/preloading-images-with-jquery。看看答案。 :) – 2012-08-08 21:41:12

回答

2

負載使用JavaScript的圖像,然後你可以使用圖像的onLoad屬性:

Image1 = new Image(); 
Image1.src = 'photo.gif'; 

/* Code here to display loading hour glass etc */ 

Image1.onload = function() { 
          /* Image has loaded here */ 
         } 
2
$("img.loader").show(); 
$("img.big").ready(function() { 
    $("img.loader").hide(); 
}): 
0

添加「點擊」事件到你的鏈接,其中通過setTimeout的顯示您的加載圖片。例如。

<a href="...some slow loading page" onclick="setTimeout(showLoading,1)">Link Text</a> 

function showLoading() { 
    // Code to show "Loading..." 
} 
相關問題