Skip to content
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

Background Images Request #19

Closed
whittyskitty opened this issue May 21, 2018 · 5 comments
Closed

Background Images Request #19

whittyskitty opened this issue May 21, 2018 · 5 comments

Comments

@whittyskitty
Copy link

Hey Malchata,

I followed along with you here: https://developers.google.com/web/fundamentals/performance/lazy-loading-guidance/images-and-video/

Within that article, you had the ability to use lazy loading with Background Images. Is there a way to use that similarly with yall.js?

Thanks

@malchata malchata self-assigned this May 21, 2018
@malchata
Copy link
Owner

I think that's a good idea, but it's not currently in yall.js. It's something I've been thinking of, but the implementation is definitely not straight forward when you start taking things like media queries into account.

I can add this to the backlog, but I'm definitely not looking to get it into yall.js super soon.

@malchata
Copy link
Owner

Hi, @whittyskitty. I've implemented this feature in a pre-release version of yall.js: https://raw.githubusercontent.com/malchata/yall.js/master/dist/yall-2.1.0.min.js

For the usage pattern, check out the test HTML file in the repo.

@martijn80
Copy link

Hi malchata, thanx for this background feauture.

Is there any reason why this can't work with data attributes just like you with regular images?
Like so?
<div class="lazy-bg" data-src="/img/test-1536w.jpg"></div>

@malchata
Copy link
Owner

malchata commented Aug 16, 2018

Because CSS. :)

The short explanation: Devices, their screens, and all the different assets we might need to load for them would make it too onerous to implement in the way you've described while not breaking anything. Relying on CSS is much more resilient, because its behavior is predictable.

The long explanation: I could go with the pattern you specified and have users specify image(s) in a data-src attribute, but that's a problem. What if a CSS image needs to be density-corrected for different screens? What if an image is art-directed? There's just too much that can change. Sure, I could write it this way, but accommodating for all the different possible scenarios would bloat this library significantly, and something could still break. So rather than fight CSS, my approach relies on it.

The path I've chosen is to hook elements users want to lazy load background images for into yall.js by way of a lazy-bg class. So literally any element that has this class is now something yall.js will start paying attention to (this class name can be overridden with options.lazyBackgroundClass). Let's say you use yall.js to lazy load a large background image for a full bleed heading on a new section using a class of full-bleed-section-masthead (wordy, I know):

.full-bleed-section-masthead {
  width: 100%;
  height: 20rem;
}

In your markup, you'll attach a class of lazy-bg to it to signify to yall.js that you want lazy load its background image when it's within range of the viewport (within 200 pixels of the viewport's edge by default, but this can be overridden by options.threshold). When this happens, yall.js will tag that element with a class of lazy-bg-loaded (this can be overridden with options. lazyBackgroundLoaded). From here, you can add some CSS like this:

.full-bleed-section-masthead.lazy-bg-loaded {
  background-image: url("/images/my-image-small-1x.jpg");
}

Of course, this is the simplest example. Let's assume this image is mobile-first styling on a 1x DPR screen. We'll need to add the following (simplified) rules to make this work a bit better:

@media (min-resolution: 192dpi) { 
  .full-bleed-section-masthead.lazy-bg-loaded {
    background-image: url("/images/my-image-small-2x.jpg");
  }
}

/* 600px and above */
@media (min-width: 37.5em) {
  .full-bleed-section-masthead.lazy-bg-loaded {
    background-image: url("/images/my-image-medium-1x.jpg");
  }
}

/* 600px and above + 2x DPR */
@media (min-width: 37.5em) and (min-resolution: 192dpi) {
  .full-bleed-section-masthead.lazy-bg-loaded {
    background-image: url("/images/my-image-medium-2x.jpg");
  }
}

/* 960px and above */
@media (min-width: 60em) {
  .full-bleed-section-masthead.lazy-bg-loaded {
    background-image: url("/images/my-image-large-1x.jpg");
  }
}

/* 960px and above + 2x DPR */
@media (min-width: 60em) and (min-resolution: 192dpi) {
  .full-bleed-section-masthead.lazy-bg-loaded {
    background-image: url("/images/my-image-large-2x.jpg");
  }
}

Oh, and what if you're not just lazy-loading a background-image resource? There are other resources that accept a url data type, such as list-style-image (not that you would ever lazy load list bullet images, but you could).

Why does this even work? Because unlike images loaded in HTML browsers speculate when it comes to loading resources in CSS. If the document loads a style sheet with a background-image referenced in it, but the document's CSSOM doesn't include a selector that references that style, the document won't load the image. This gives yall.js the ability to effectively lazy load the resource the way that it does.

I hope this helps!

@martijn80
Copy link

Thanks for your extensive reply. Learned a lot.

malchata added a commit that referenced this issue Dec 15, 2018
- Added linting via eslint.
- Updated Babel to version 7.
- Added @babel/preset-env. May add a separate leaner build for mjs in the future.
- Resolved bugs #24 and #28 by removing async decoding via `Image.decode()`.
- Added features requested in #19 and #32.
- Updated documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants