Created
June 15, 2022 17:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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