Image Loading Animation in JavaScript

Image Loading Animation in JavaScript
Code Snippet:Imágen cargada
Author: Reynaldo David House Tomas
Published: January 6, 2024
Last Updated: January 6, 2024
Downloads: 341
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code creates an image-loading animation. It progressively blurs an image while displaying the loading percentage. It helps visualize the loading progress of an image.

You can use this code on websites to enhance user experience during image loading. It provides a visual loading indicator for smoother interactions.

How to Create Image Loading Animation in JavaScript

1. First of all, set up the HTML structure with a container for the image and a text element to display the loading percentage.

<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Blurry Loading</title>

    <section class="bg"></section>
    <div class="loading-text">0%</div>
    <script src="script.js"></script>

2. Apply basic styling to the elements, adjusting fonts, positioning, and background blur. You can modify the CSS styles according to your needs.

@import url('');

* {
  box-sizing: border-box;

body {
  font-family: 'Ubuntu', sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;
  margin: 0;

.bg {
  background: url('')
    no-repeat center center/cover;
  position: absolute;
  top: -30px;
  left: -30px;
  width: calc(100vw + 60px);
  height: calc(100vh + 60px);
  z-index: -1;
  filter: blur(0px);

.loading-text {
  font-size: 50px;
  color: #fff;

3. Finally, use JavaScript to manipulate the loading text and apply blur effects. It sets up a function blurring() that increases the loading percentage and adjusts the blur effect accordingly.

const loadText = document.querySelector('.loading-text')
const bg = document.querySelector('.bg')

let load = 0

let int = setInterval(blurring, 30)

function blurring() {

  if (load > 99) {

  loadText.innerText = `${load}%` = scale(load, 0, 100, 1, 0) = `blur(${scale(load, 0, 100, 30, 0)}px)`

const scale = (num, in_min, in_max, out_min, out_max) => {
  return ((num - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min

That’s all! hopefully, you have successfully created a blurred image-loading animation on your webpage. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link