0

さて、私はこの "slideshow.html" にたくさんの写真と "index.html" を含めました。

index.html

<a href="">click</a>
<ul></ul>

slideshow.html

<li><img src="1.jpg" alt="" /></li>
<li><img src="2.jpg" alt="" /></li>
<li><img src="3.jpg" alt="" /></li>

そして、私はこのようなスクリプトを持っています;

$(document).ready(function(){
            $('a').click(function(){
            $('ul').append('<li id="preloader"><img src="preLoader.gif" /></li>');
                   $('ul').load('slideshow.html',function(){
                           $('#preloader').remove();
                   });
            });
});

クリックして preloader.gif を追加し、load メソッドを呼び出して、画像から slideshow.html が読み込まれた後、アニメーションを削除します。私のスクリプトを使用すると、ページが読み込まれますが、画像が完全に読み込まれる前にアニメーションが削除されます:(ありがとう

4

2 に答える 2

2
$(document).ready(function(){
    //anchor click
$('a').click(function(){
    //empty the div
    $('div').empty();
            //perform ajax request
    $('div').load('toLoad.html',function(){
                  //hides all loaded images
        $('div.imageHolder img').hide();
                  //applies preloader for each image loaded
        $('div.imageHolder img').each(function(){
            //creates new image object
            var img = new Image();
                            //the current image src is stored in sursa variable
            var sursa = $(this).attr('src');
                            //the current image parent is stored in parent var 
            var parent = $(this).parent();
                            //the load animation is appended to current image parent 
            parent.append('<img src="blueLoader.gif" alt="loader" />');
           //loading image css settings
            $('img[alt="loader"]').css({'display':'block','margin':'10px auto'});
                           //this is the key
            $(img).load(function(){
                            //after image is loaded
                parent.append($(this));
                $(this).hide().fadeIn(500).css({'width':'200px','height':'80px'});
                $(this).siblings().remove();
            }).attr('src',sursa);


        });

    });
    return false;
}); 
   });
于 2009-10-16T18:35:52.033 に答える
0

画像のプリロードは、これとは異なる方法で行われます。jQuery では、次のようなことができます (未テスト):

$('<ul><li id="image1"><img id="throbber1" scr="preLoader.gif" /></li></ul>').appendTo('body');
var $img = $(new Image()).attr('src', '1.jpg').appendTo('ul li#image1');
$img.load(function() {
    $('#throbber1').hide();
});
于 2009-10-16T17:33:28.740 に答える