Pergunta

Eu tenho algumas imagens em uma página que são carregados aleatoriamente e eles são mais de 100kbs, é possível tê-la totalmente carregada, fade-lo em, ao invés de incluir progressivamente carregá-lo?

Meu JS parece com isso...

(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);
Foi útil?

Solução

Deve ser capaz de, só definir a imagem para display:none na sua folha de estilos e modificar o bit de script que define a fonte para isso:

$(this).attr( { src: fullPath }).load(function() {
  $(this).fadeIn()
});  

Outras dicas

Iniciar com as imagens escondidas, usando CSS.Então use:

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

e tem o efeito de fade-in executar lá.

Não há outro MODO a questão que discute o pré-carregamento de imagens utilizando # jQuery aqui: Pré-carregamento de imagens com jQuery

Citando a partir do topo resposta:

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'
]);

se você deseja que todas as imagens de pré-carga antes de desaparecer, e exibir uma mensagem carregando para o usuário, você pode usar algo como isto:

                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);
                });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top