ロードを終了した後のみの画像のフェード
-
16-12-2019 - |
質問
私はランダムにロードされて100kbを超えるページ上のいくつかの画像がそれを完全にロードされていることを可能にすることが可能ですそれを次第にロードするのではなく、それを遅らせることは?
私の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);
. 解決
は、スタイルシートのdisplay:none
に画像を設定し、これにSRCを設定するスクリプトのビットを変更するだけです。
$(this).attr( { src: fullPath }).load(function() {
$(this).fadeIn()
});
. 他のヒント
CSSを使用して隠された画像から始めます。それから:
$(document).ready(function() {
// Code goes here.
});
.
でフェードインを実行してください。
ここでのjQueryを使用して画像をプリロードする問題があるので、 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'
]);
. フェージング前にすべての画像をプリロードしてユーザーにロードメッセージを表示する場合は、次のようなものを使用できます。
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);
});
. 所属していません StackOverflow