Skip to content

Instantly share code, notes, and snippets.

@jnicol
Last active June 28, 2026 23:45
Show Gist options
  • Select an option

  • Save jnicol/d5434130c8ae4893f332aae284bb35dc to your computer and use it in GitHub Desktop.

Select an option

Save jnicol/d5434130c8ae4893f332aae284bb35dc to your computer and use it in GitHub Desktop.
Animate native loading="lazy" images
Source: https://medienbaecker.com/articles/animate-native-lazy-loading
<style>
@media (scripting: enabled) {
[loading="lazy"] {
opacity: 0;
transition: opacity 0.5s;
}
}
</style>
<img src="image.jpg" width="800" height="600" loading="lazy">
<script>
document.querySelectorAll('[loading="lazy"]').forEach(element => {
const animateIn = () => {
element.style.opacity = 1;
};
if (element.complete) {
animateIn();
} else {
element.addEventListener('load', animateIn);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment