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

How to detect background images only? #274

Closed jinyus closed 2 years ago

jinyus commented 6 years ago

I hope my question was clear...by default it detects background images along with . Is it possible to detect only when bg images are loaded?

desandro commented 6 years ago

Sorry, there is no API to only check for background images. But you can monkey-patch a solution. Add this code:

ImagesLoaded.prototype.addElementImages = function( elem ) {

  // get background image on element
  if ( this.options.background === true ) {
    this.addElementBackgroundImages( elem );
  }

  // get child background images
  if ( typeof this.options.background == 'string' ) {
    var children = elem.querySelectorAll( this.options.background );
    for ( i=0; i < children.length; i++ ) {
      var child = children[i];
      this.addElementBackgroundImages( child );
    }
  }
};