-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
BgSet Can't use multiple backgrounds and there is no resize update event #827
Comments
I haven`t fully understand your use case, but maybe the following change could help: We currently dispatch the following event: var event = lazySizes.fire(elem, 'bgsetproxy', {
src: bg,
useSrc: regBgUrlEscape.test(bg) ? JSON.stringify(bg) : bg,
});
if(!event.defaultPrevented){
elem.style.backgroundImage = 'url(' + event.detail.useSrc + ')';
} Would the following change help? var event = lazySizes.fire(elem, 'bgsetproxy', {
src: bg,
useSrc: regBgUrlEscape.test(bg) ? JSON.stringify(bg) : bg,
});
if(!event.defaultPrevented){
elem.style.backgroundImage = event.detail.fullSrc || 'url(' + event.detail.useSrc + ')';
} With this change you could write something like this: document.addEventListener('bgsetproxy', ({target, detail}) => {
const additionalBg = targettarget.dataset.additionalBg;
if (additionalBg) {
detail.fullSrc = `url(${detail.useSrc}) ${additionalBg}`;
}
}); From current code you could do something (less/still) ugly like this: const wait = Promise.resolve();
document.addEventListener('bgsetproxy', async ({target}) => {
const additionalBg = target.dataset.additionalBg;
if (!additionalBg) {
return;
}
await wait;
target.style.backgroundImage += additionalBg;
}); At the end I haven't understood your use case and did no test my code above. But I can say, that I have written bgset to enable responsive images in combination with |
So perhaps what's missing from describing my use case is that my CSS includes There are a lot of neat tricks you can achieve with background images that don't have direct parallels that can be used with In addition to situations in which backgrounds can be used for things that images can't, I believe background images still have a place even though I really appreciate this plugin because I can make those background images responsive, even though I may use I haven't tried your suggested fix yet, but it looks promising. Perhaps the |
Your explanation makes totally sense. For me of course interesting is wether the code change outlined in my previous post would make any sense to you? I'm willing to change the code to something like this. And yeah I will then also document |
I just added the fullSrc change to the following branch: https://github.com/aFarkas/lazysizes/tree/feature/bgset/fullsrc-bgsetproxy It would be nice if you could test the change and confirm that this solves your issue. |
Thanks for this, @aFarkas . Hoping I have an opportunity to check this out later this week, or perhaps next week. |
This is working for me @aFarkas. Thank you! |
BGSet: Add `fullSrc` prop to the `bgsetproxy` event (fixes #827)
This is not so easy to use, I tried to understand the discussion between the two gurus, and documented it. First, use Modify var proxyLoad = function(e){
if(!e.target._lazybgset){return;}
var image = e.target;
var elem = image._lazybgset;
var bg = image.currentSrc || image.src;
var prefix = elem.getAttribute('data-bgprefix'); // Gets prefix string
if(bg){
var useSrc = regBgUrlEscape.test(bg) ? JSON.stringify(bg) : bg;
var event = lazySizes.fire(elem, 'bgsetproxy', {
src: bg,
useSrc: useSrc,
fullSrc: prefix, // Set it
});
if(!event.defaultPrevented){
elem.style.backgroundImage = event.detail.fullSrc || 'url(' + event.detail.useSrc + ')';
}
}
... Then go back to the html setting Ex: <div class="d-md-block lazyload" data-bgset="./images.jpg [(min-width: 768px)]" data-bgprefix="url(./images.jpg), linear-gradient(to bottom, #efaf04 0%, #ff9c39 100%)"></div> Is mean in a browser min-with 768px, the image preload will be executed and the style I don't know if I'm using |
The css
background-image
property supports a comma separated list of images, but there's no way to make use of lazy sizes along with multiple backgrounds.In my particular use case I'm pairing an image with a linear gradient, eg
background-image: url(foo.jpg), linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.4))
. I would like to be able to lazy load and swap out the first part, while maintaining the second.I'm not sure this particular use case warrants a brand new syntax to be able to pass multiple values however I would like to be able to append the extra background after lazy sizes does it's thing. I can add a 'lazyloaded' event listener to capture the first image load after page load, however when the image is swapped on resize the work here is undone.
A possible solution to this would be to have a 'resizeImageUpdate` event or something of the like, however nothing of the sort currently exists.
I solved my problem with a MutationObserver, but I think there ought to be a better way. Surely an update event would have other potential uses beyond my niche use case.
My solution below where the
data-additional-bg
attribute on the element holds the rest of thebackground-image
string:The text was updated successfully, but these errors were encountered: