Hi,
the plugin already comes with a fade in (300ms) – if you want to modify the styles, you can use the filter (see the FAQ https://ww.wp.xz.cn/plugins/lazy-loading-responsive-images/#faq).
If you enable the plugion option to load the unveilhooks plugin of lazysizes, it also supports background images, but you need to modify the markup for that on your own, like described here: https://github.com/aFarkas/lazysizes/tree/gh-pages/plugins/unveilhooks
To adjust the lazyload threshold, you can modify the lazySizesConfig.expand option (https://github.com/aFarkas/lazysizes#js-api—options). Should look something like that (not tested):
function slug_enqueue_lazysizes_options() {
wp_add_inline_script( 'lazysizes', 'window.lazySizesConfig = window.lazySizesConfig || {}; lazySizesConfig.expand = 100;', 'before' );
}
add_action( 'wp_enqueue_scripts', 'slug_enqueue_lazysizes_options' );
Hope that helps!
Florian
Thread Starter
Rookie
(@alriksson)
To start with thanks for the answers.
1. and 3. is easy yes thanks.
But 2. please tell me if I’m wrong, what d I need to do, add following class for all css-background images bg-stage lazyload and then add the JS
window.lazySizesConfig = window.lazySizesConfig || {};
window.lazySizesConfig.requireJs = function(modules, cb){
window.require(modules, cb);
};
Would be a hassle to manually add to all css-bckground images. And a few will be hard due to how the theme is coded.
Hey,
the JS can be skipped – that is only needed if you want to lazyload modules. What you need to do is: modify the markup of the elements that have background images so it looks like that:
<div class="lazyload" data-bg="bg-img.jpg">
Are those background images part of the normal post content? Or, for example, in the header of the theme? It might be possible to support automatic lazy loading for background images in the post content, I will take a look at it.
Best,
Florian
Thread Starter
Rookie
(@alriksson)
Ok, please do that! 🙂
Any news if it’s possible to automatic the process?
I think it should be possible to search for background-image inline styles and modify them, but because it is also possible that the image is inside a background rule with other values, it might be tricky to automatically modify all background images in the content.
Currently, I do not have the time to test this, but it is on my list.
Thread Starter
Rookie
(@alriksson)
Ok I understand the complexity.. no one seems to have it get which would help me a lot since many images in themes nowadays is going to be outputted as css background image.
Hi @alriksson,
I created a first version of background image support (https://github.com/florianbrinkmann/lazy-loading-responsive-images/releases/tag/v3.5.0-alpha.1 – the lazy-loading-responsive-images.zip). It should support background images that are added via background-image inline style.
The issue: users with disabled JS will not see the background iamge.
If that is not a problem for you, would you give the version a try? Note: you need to enable the unveilhooks plugin option in the plugin settings.
Best,
Florian
Thread Starter
Rookie
(@alriksson)
Hey sounds interesting but we rebuild a few things on the site since no one solved it. It needs to be <noscript> fall back for SEO so that is not an option to not display if JS is disabled.
Keep on working! but not urgent from our site right now. Header images won’t be lazyloaded since they are within the threshold anyhow.
All right 🙂 If I stumble upon a solution some time, I will mention it here.