3

スライド ショー用の大きな画像をページに追加していますが、ページの通常の部分 (画像を含む) が完全に読み込まれたときにのみ、これらの画像の読み込みを開始したいと考えています。

そのために、$(window).load()関数に画像を追加しています。

var slide_total = 20;

$(window).load(function() {

    for (i = 2; i <= slide_total; i++)
    {
        content = '<li><img src="/images/header' + ((i < 10) ? '0' : '') + i + '.jpg" width="960" height="314"></li>';
        $("#slideshow li:last-child").after(content);
    }

    slide_interval = setInterval( "slideSwitch()", slide_duration );

});

スライド ショーslideSwitch()はすべての画像が完全に読み込まれると開始するはずですが、現状では、要素が DOM に追加された瞬間に開始されます。

document.readyスライドショーが通常の画像の読み込みを妨げたくないので、ループを関数に移動できません。

間隔を設定する前に、すべての画像が読み込まれたかどうかを確認するにはどうすればよいですか?

4

1 に答える 1

4

これを試して:

// get the total number of images inserted in the DOM
var imgCount = $('#slideshow img').length;

// initialize a counter which increments whenever an image finishes loading
var loadCounter = 0;

// bind to the images' load event
$("#slideshow li img").load(function() {

    // increment the load counter
    loadCounter++;

    // once the load counter equals the total number of images
    // set off the fancy stuff
    if(loadCounter == imgCount) {
        slide_interval = setInterval( "slideSwitch()", slide_duration );
    }
}).each(function() {

    // trigger the load event in case the image has been cached by the browser
    if(this.complete) $(this).trigger('load');
});
于 2010-09-08T14:58:19.537 に答える