2017-01-10 72 views
0

我很新的JavaScript/jQuery。 下面的示例確實會爲我的列表中的每個瀑布項調用.each部件。但是$img.load永遠不會因爲某種原因而被調用?Image.load函數沒有被調用

無法真正弄清楚我做錯了什麼?

(function($) { 
 
    $(document).ready(function(e) { 
 

 
    var $waterfall = $('.waterfall'); 
 
    if ($waterfall.length) { 
 
     $waterfall.waterfall({}); 
 
    } 
 

 
    // sort the waterfall when images are loaded 
 
    var $waterfallItem = $('.waterfall-item'); 
 
    $waterfallItem.each(function(j, image) { 
 
     var $img = $(image); 
 

 
     $img.load(function() { 
 
     $waterfall.waterfall({}); 
 
     }); 
 
    }); 
 

 
    }); 
 
})(jQuery);
<ul class="waterfall"> 
 
    <li class="waterfall-item"> 
 
    <a href="hidden link" title="hidden title"> 
 
     <img alt="hidden alt" title="hidden title" data-srcset="hidden in this example" data-src="also hidden in this example" src="still hidden in this example" data-sizes="(min-width:440px) 300px, 95vw" class=" lazyloaded" sizes="(min-width:440px) 300px, 95vw" 
 
     srcset="think I'll hide this one too"> 
 
     <span class="waterfallImgOverlay"></span> 
 
    </a> 
 

 
    </li> 
 
</ul>

+1

檢查參考加載功能https://api.jquery.com/load-event/ – user1097772

+0

我已經看過那個頁面,但並不真正瞭解所有內容或知道我在找什麼。 –

+1

你試過把你的jquery代碼放在'$(document).ready(function(){})'裏面。 – Manwal

回答

0

解決這樣的:

var $waterfall = $('.waterfall'); 
 
if ($waterfall.length) { 
 
    $waterfall.waterfall({}); 
 
} 
 

 
// sort the waterfall when images are loaded 
 
var $waterfall_images = $waterfall.find('img'); 
 
$waterfall_images.each(function(j, image) { 
 
    var $img = $(image); 
 

 
    $img.load(function() { 
 
    $waterfall.waterfall({}); 
 
    }); 
 

 

 
});