GithubHelp home page GithubHelp logo

Comments (5)

malchata avatar malchata commented on May 25, 2024 2

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!

from yall.js.

martijn80 avatar martijn80 commented on May 25, 2024 1

Thanks for your extensive reply. Learned a lot.

from yall.js.

malchata avatar malchata commented on May 25, 2024

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.

from yall.js.

malchata avatar malchata commented on May 25, 2024

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.

from yall.js.

martijn80 avatar martijn80 commented on May 25, 2024

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>

from yall.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.