Web component / esm

<lazy-img>

README · All demos

Class-only import with manual registration

This page loads the component class from esm.sh and registers it if the tag isn’t already defined. Without JavaScript, or if the module can’t load, the optional image isn’t created; its recovery link remains available. Keep essential images native.

<script type="module">
import { LazyImgElement } from
  'https://esm.sh/@aarongustafson/lazy-img@2.1.3/lazy-img.js?bundle&target=es2022';
if (!customElements.get('lazy-img')) {
  customElements.define('lazy-img', LazyImgElement);
}
</script>

<lazy-img src="./sample.svg"
  alt="Mountain trail at sunrise illustration">
</lazy-img>
<p>If the optional image is unavailable,
  <a href="./sample.svg">open an alternative image</a>.
</p>

If the optional image is unavailable, open an alternative image.