-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add background image support through optional background param #174
Conversation
Yeah! |
Thanks so much for kicking this off. I've been reluctant to get into this. Having someone step up to get the ball rolling is a huge morale boost. Your contribution is a good start, but I feel there's plenty of more work to be done. Some issues
1 is a simple fix. 2 is a bit more complex, but can be done. 3 requires serious consideration. It would mean changing the API to allow for this functionality. So I'm considering doing a bigger overhaul that could accommodate background images in a way that I feel completely addresses these issues. I've started |
Great feedback @desandro! Some thoughts...
If you really needed to know when all of an element's background images were loaded you could write something like: var container = document.querySelector( '#container' );
var element = container.querySelector( 'div' );
var imgLoader = new imagesLoaded( container , { background: true } );
var progress = 0;
var regex = /\burl\s*\(\s*["']?([^"'\r\n\)\(]+)["']?\s*\)/gi;
var total = element.style.background.match( regex ).length;
imgLoader.on( 'progress' , function ( instance, image ) {
if ( image.img.originalElement === element) {
progress++;
}
if ( progress >= total ) {
// all background images are loaded for that element...
}
}); Or, even simpler... just create a new var elements = document.querySelectorAll( '.bg-image' );
for ( var i=0, len = elements.length; i < len; i++ ) {
var imgLoader = new imagesLoaded( elements[i], { background: true } );
imgLoader.on( 'done', function( instance ) {
// all background images are loaded for that element...
});
} Again, to me, it just seems simpler if the developer writes this kind of specific use-case code. Either way, please definitely feel free to look over the code again as I've rewritten history a bit here (ie. rebased, squashed, force-pushed etc.) and let me know your thoughts. |
Cool! This support is exactly what I am looking for! Would be a great help for me. Thank you all! |
@desandro Like I said above, I think this is a good stop-gap for any kind of bigger refactor you're going to do in terms of firing events on both an image load and all images loaded on a multi-background image element. Definitely let me know if there's something I'm missing or anything terribly wrong with this implementation. Keys here are that it doesn't change the API at all but just adds unobtrusive support for background images. Note: Again, the code was updated from the original pull request (and probably the last time you perused it)... I rebased and squashed my commits down so that there was still only a single commit (apologize beforehand as it doesn't make it very easy to see where I made changes). |
Adding background images is a huge API change. While this may work as a stop gap, I don't feel comfortable implementing a stop-gap solution like this for such an important feature.
In short, if we're going to add this feature, I want to be comprehensive about it. I realize the waiting game is painful. But good news is that I have more personal availability to devote to code. I do appreciate your patience. This will happen. |
Should I just close this PR then? |
Let's keep it open. It'll help others to chime in or use your fork in the meantime. |
Sounds good. Removed background image detection of child elements, for my own sanities sake. |
Ping @desandro... it looks like no work has been done on this project in awhile and I'd like to think we don't want to fragment efforts. It looks like there's a number of PRs that could be merged in safely/easily. Any update, specifically, on the work relevant to this PR? I guess I still don't see why the work I've done can't be merged in. |
Hello! Thanks for the ping. Man, I've let things get unkept here. Frustration is justified. Thinking it over, I feel best course of action is to maintain your own fork if you want to see things moving. I don't feel ready to start making changes and I don't want to hold separate progress back. This issue is just one of several that's I'm on the fence with. I needed imagesLoaded to support Masonry & other libraries, but its usage has grown outside that:
The bigger issue is whether or not imagesLoaded to should support stuff that Masonry + co. don't really need. |
How stable is the background branch? Think it would be safe for production? |
🎉 ⭐ 🌈 imagesLoaded v3.2.0 now supports background images 🌈 ⭐ 🎉 Try it out! Thanks again for getting the ball rolling on this. Closing this PR now. Set // jQuery
$('#container').imagesLoaded( { background: true }, function() {
console.log('#container background image loaded');
});
// vanilla JS
imagesLoaded( '#container', { background: true }, function() {
console.log('#container background image loaded');
}); Set to a selector string like // jQuery
$('#container').imagesLoaded( { background: '.item' }, function() {
console.log('all .item background images loaded');
});
// vanilla JS
imagesLoaded( '#container', { background: '.item' }, function() {
console.log('all .item background images loaded');
}); |
So I've gotten a bit tired of seeing continued discussion on a very old issue (#29); Either way, I decided to do something about it. I've included
background-image
support through an optionalbackground
param that you can set when creating a newimagesLoaded
instance. Examples of how to use this:The implementation for this has been simplified to allow for the most flexibility. That said, it’s better to be specific with your selector strings (as shown in example 1). Otherwise, we have no way of filtering an elements children and are subsequently forced to iterate and check each.
Please let me know if you’d like me to make any changes to the implementation, add more tests etc..