Giter Club home page Giter Club logo

lazy-image's Introduction

Published on webcomponents.org npm (scoped) Build Status Contact me on Codementor

<lazy-image>

Lazily load your images!

πŸš› Get it!

npm i -S @power-elements/lazy-image

πŸ“¦ Load it!

<!-- From CDN -->
<script async type="module" src="https://unpkg.com/@power-elements/lazy-image/lazy-image.js"></script>

<!-- From local installation -->
<script async type="module" src="/node_modules/@power-elements/lazy-image/lazy-image.js"></script>

<!-- In a Module -->
<script type="module">
  import '/node_modules/@power-elements/lazy-image/lazy-image.js';
  // ...
</script>

πŸ’ͺ Use it!

<lazy-image src="image.jpg" alt="Lazy Image">
  <svg slot="placeholder"><use xlink:href="#placeholder-svg"></use></svg>
</lazy-image>

The optional placeholder could be any element. Inline SVG, Pure CSS graphics, or an <img src="data:foo"/> would work best.

πŸ’„ Style it!

You should give your <lazy-image> elements some specific dimensions, since it absolutely positions its shadow children. In most cases, you should set the wrapping element as well as the --lazy-image- custom properties to the known display dimensions of your image.

<style>
html {
  --lazy-image-width: 640px;
  --lazy-image-height: 480px;
}

lazy-image {
  width: var(--lazy-image-width);
  height: var(--lazy-image-height);
}
</style>

<lazy-image src="https://fillmurray.com/640/480"></lazy-image>

<lazy-image> exposes a set of custom properties for your customizing delight:

Property Purpose Default
--lazy-image-width Width of the internal image and placeholder elements 100%
--lazy-image-height Height of the internal image and placeholder elements 100%
--lazy-image-fit object-fit property of the internal image and placeholder elements contain
--lazy-image-fade-duration Duration of the fade in from the placeholder to the image. Set to 0 to disable fading. 0.3s
--lazy-image-fade-easing ease property of the opacity transition for the image and placeholder ease

Browser support

lazy-image manages the loading of your images via an Intersection Observer. In browsers where an Intersection Observer is not present, your images will be loaded immediately much like standard <img/> elements. Conditionally delivering the IntersectionObserver polyfill along with your lazy-images to your users will ensure that all users experience the benefits of loading images lazily. Stay lazy, friend!

lazy-image's People

Contributors

almenon avatar bennypowers avatar dependabot[bot] avatar eavichay avatar peschee avatar semantic-release-bot avatar westbrook avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lazy-image's Issues

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Disconnect observer

I notice you dont disconnect the observer before deleting it. I dont think deleting the property will disconnect it for you.

Question: thoughts on adding code to maintain display ratio?

I'd be very interested in seeing a CSS variable that supported the maintenance of an image's display ration before it was loaded. The goal there being to keep the content from jumping when the actual image is brought in.

I could see it being managed a number of different ways, but am not sure if it really aligns with the main goals of your component. Probably the most straight forward would be to use some sort of maintainRatio property, so that it would be available in your templating. This would allow for a secondary version of the styling that make the main element position: relative; padding-bottom: var(--ratio-padding); and the image position: absolute; top: 0; left: 0; for example. However, I'm not sure whether that starts to overcomplicate things.

[fade], maybe I'm doing it wrong?

I'm having trouble getting the fade functionality to perform correctly, and wondered if there was something special I need to do beyond setting the attribute?

From the code it looks like the "fade in" would be managed by the following code:

    img[intersecting] {
      opacity: 1;
    }

However, this doesn't guarantee that the image is available to display over the course of the image fade in. I'm fairly certain that you'd need to include something on the order of:

  loadImage() {
    let image = new Image();
    image.onload = () => {
      this.__img.src = this.src;
      requestAnimationFrame(() => {
        requestAnimationFrame(() => {
          this.__img.setAttribute('loaded','');
        });
      });
    };
    image.src = this.src;
  }

And call it from the intersecting setter like:

  set intersecting(intersecting) {
    this.__intersecting = intersecting;
    if (!this.__img) return;
    if (intersecting) {
      this.__img.setAttribute('intersecting', ''),
      this.loadImage();
    } else this.__img.removeAttribute('intersecting')
  }

It's possible I'm just completely on the wrong track, but if this is interesting I'd be happy to supply a PR.

When `!window.IntersectionObserver` errors are thrown.

It looks like you're missing coverage for when IntersectionObserver isn't available in the browser. Larger scale, it might be fun to have some sort of fallback to a polyfill, but I'd settle for it just loading the assets at runtime for now.

It looks like when you add the following to initIntersectionObserver:

    if (!window.IntersectionObserver) {
      this.setIntersecting(true);
      return;
    }

and swap the method order in connectedCallback, like so:

  connectedCallback() {
    this.attachShadow({mode: 'open'})
      .appendChild(getTemplate(this).content.cloneNode(true));
    this.initIntersectionObserver(this);

    this.placeholder = this.getAttribute('placeholder')
  }

That this functionality is achieved.

If that seems copacetic to you, I'm happy to submit a PR, or if there is a different approach, I'd be happy to take notes or wait for your our addition to support this area.

Thanks!

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.