Skip to content

Instantly share code, notes, and snippets.

@heyMP
Created June 15, 2022 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heyMP/4f05eafd2a2db7e78622e40fd1d6aa1d to your computer and use it in GitHub Desktop.
Save heyMP/4f05eafd2a2db7e78622e40fd1d6aa1d to your computer and use it in GitHub Desktop.
@btopro shared a cool way of using the Image() API to lazyload images
if (attr === "srcconverted" && this.src != "" && !this.rendering) {
this.rendering = true;
// loads the image in the background in-case of quality change
// also then supports failure events
let i = new Image();
i.onload = () => {
this.render(this.srcconverted);
};
// try loading just the normal one if this bombed
i.onerror = () => {
this.render(this.src);
};
i.src = this.srcconverted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment