你需要做確保您的代碼在DOM準備就緒後加載。
window.onload = function(){
var fadeIn = function(obj) {
var item = $(obj).parent();
item.fadeIn(1000);
};
};
...這是一個部分修復,但不是真的,因爲在所有的可能性,你的<img/>
標籤與在onload硬編碼到他們會設法大火,方法是可用之前。
由於您使用jQuery無論如何,你應該看看這樣的:
$(function() {
$(".item img").load(function(){
$(this).fadeIn(1000);
});
});
,並從你的img標籤擺脫硬編碼的onload的。
應該注意的,也有在.load()API頁面上列出的注意事項:
從文檔(http://api.jquery.com/load-event/):
Caveats of the load event when used with images
A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:
It doesn't work consistently nor reliably cross-browser It doesn't fire correctly in WebKit if the image src is set to the same src as before It doesn't correctly bubble up the DOM tree Can cease to fire for images that already live in the browser's cache
一個更好的解決方案可能是隱藏與CSS的圖片,加載事件被激發在此之後,淡出他們進來
(甚至比這更好的可能是讓瀏覽器的行爲是因爲你的結果可能會混合在一起,而用戶看到空白頁面時可能會彈出重新加載按鈕,因爲他們的瀏覽器會閃爍在你的動畫完成之前進行。 ...只是一個善意的警告,這些類型的DOM操作中有時可有用戶行爲意想不到的副作用!)
如果jQuery的作品,將在IMG onload事件導致雙命中,導致它淡入兩次? –
我認爲他們將被覆蓋。 –