1

ページにランダムに読み込まれる画像があり、それらは 100kbs を超えています。段階的に読み込むのではなく、完全に読み込み、フェードインすることはできますか?

私のJSはこのように見えます...

(function($){   
$.randomImage = {
    defaults: {         
        //you can change these defaults to your own preferences.
        path: '_images/', //change this to the path of your images
        myImages: ['hero_eagle.jpg', 'hero_giraffe.jpg', 'hero_owl.jpg', 'hero_rabbit.jpg']
    }           
}   
$.fn.extend({
        randomImage:function(config) {              
            var config = $.extend({}, $.randomImage.defaults, config);              
             return this.each(function() {                      
                    var imageNames = config.myImages;                       
                    //get size of array, randomize a number from this
                    // use this number as the array index
                    var imageNamesSize = imageNames.length;
                    var lotteryNumber = Math.floor(Math.random()*imageNamesSize);
                    var winnerImage = imageNames[lotteryNumber];
                    var fullPath = config.path + winnerImage;                       

                    //put this image into DOM at class of randomImage
                    // alt tag will be image filename.
                    $(this).attr( { src: fullPath });                                   
            }); 
        }           
}); 

})(jQuery);
4

3 に答える 3

3

できるはずですがdisplay:none、スタイルシートで画像をに設定し、srcを次のように設定するスクリプトのビットを変更するだけです。

$(this).attr( { src: fullPath }).load(function() {
  $(this).fadeIn()
});  
于 2012-03-15T12:54:24.053 に答える
2

CSSを使用して非表示にした画像から始めます。次に、以下を使用します。

 $(document).ready(function() {
   // Code goes here.
 });

そこでフェードインを実行します。

jQueryを使用した画像のプリロードについて説明している別のSOの質問があります:jQueryを使用した画像のプリロード

トップアンサーからの引用:

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
}

// Usage:

preload([
    'img/imageName.jpg',
    'img/anotherOne.jpg',
    'img/blahblahblah.jpg'
]);
于 2012-03-15T12:52:24.393 に答える
0

フェードインする前にすべての画像をプリロードし、読み込みメッセージをユーザーに表示する場合は、次のようなものを使用できます。

                var gotime = imgArray.length;
                $(".maxCount").text(gotime);

                var imgCounter = 0;
                $.each(imgArray, function(){
                    $(new Image()).load(function(){
                        imgCounter++;
                        $(".presentCount").text(imgCounter);

                        if (--gotime < 1) {
                            $("#content").fadeIn();
                        }
                    }).attr('src', this);
                });
于 2012-03-15T13:08:14.033 に答える