desandro / imagesloaded

:camera: JavaScript is all like "You images done yet or what?"
https://imagesloaded.desandro.com
MIT License
8.88k stars 1.15k forks source link

** Question ** Use in each image independly? #276

Closed zzseba78 closed 6 years ago

zzseba78 commented 6 years ago

Hi! First of all, amazing plugin, thanks for your work.

I want to implement this in each image or background image. So, as soon as the background image of each block in collection of elements ( same class for example ) loads, it add a class and animate the opacity with css ( fade In ). Is this possible?

Thanks!

desandro commented 6 years ago

Yes. use background: true and progress event

zzseba78 commented 6 years ago

Amazing! Thanks for your answer @desandro ! I did this and it´s working perfect with Jquery

$('.slide-home').imagesLoaded()
    .progress( function( instance, image ) {
    $(image.img).removeClass('uk-invisible').addClass('uk-animation-fade'); // remove transparent and fade in image
    console.log('image ' + image.img.src + ' loaded');
});

I couldn´t resolve how to achieve this in vainilla javascript, it´s possible? I´m not clear on this since this sencence: .imagesLoaded() returns a jQuery Deferred object. This allows you to use .always(), .done(), .fail() and .progress().

I can use progress only with jquery? Is there any simple working example? Thanks

desandro commented 6 years ago

Vanilla JS would look like:

var imgLoad = new imagesLoaded('.slide-home');
imgLoad.progress( function( instance, image ) {
  // remove transparent and fade in image
  image.img.classList.remove('uk-invisible');
  image.img.classList.add('uk-animation-fade');
  console.log('image ' + image.img.src + ' loaded');
});
zzseba78 commented 6 years ago

Amazing!! Thanks you!!