Se desvanece solo después de terminar la carga
-
16-12-2019 - |
Pregunta
Tengo algunas imágenes en una página que se cargan al azar y tienen más de 100kbs, ¿es posible que se cargue completamente y luego se desvanezca en lugar de cargarla progresivamente?
MI JS se parece a esto ...
(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);
Solución
debería poder, simplemente configure la imagen en display:none
en su hoja de estilo y modifique la broca del script que establece el SRC a esto:
$(this).attr( { src: fullPath }).load(function() {
$(this).fadeIn()
});
Otros consejos
Comience con las imágenes ocultas usando CSS.Luego use:
$(document).ready(function() {
// Code goes here.
});
y tenga el fade-in Ejecute allí.
Hay otra pregunta así que discute las imágenes de precargueing usando jquery aquí: Imágenes de precarga con jQuery
citando desde la respuesta superior:
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'
]);
Si desea que todas las imágenes precargan antes de desaparecer, y mostrar un mensaje de carga al usuario, puede usar algo como este:
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);
});