Build a Lazy Image Loader in JS

September 14th, 2016

I have recently been having a lot of fun teaching an interaction design class, and had been wanting to play around with building a lazy image loader that displays a blurry low resolution very small image initially, then fades in a high resolution, larger, sharp image asynchronously, after that image has loaded. You can see a good implementation of this type of effect on medium.com.

My strategy is to simply have a small blurry image load, then once the full sized image has loaded add it to the DOM and just put it right on top of the small image. The fade in effect can be achieved with just a little CSS. Take a look at this simple demo to see it in action. Let’s take a look at the code. You can see in the HTML markup, only the small image is in the file…

Then, after the body tag I added this simple JavaScript…

Line 2 creates a new image object and line 3 assigns the source. At this point, the browser will start downloading the source file from the server. The anonymous function on line 5 runs once the image has completely downloaded – however long that takes. This function then creates a new image element, assigns the source file to it (the one we just downloaded and is now in the cache). It then assigns two classes to the image and adds it to the header, which has an ID of loader.

Let’s take a quick look at the two classes that were added.

You can see that the fullsized class is absolutely positioning the image on top of the low resolution blurry image. The fade class fades it in, giving an effect of going from blurry to clear. Note that I have left all the browser prefixes off the fade and keyframes rules for simplicity.

It is interesting to point out, that in this case, the images are actually elements on the page. This technique works just as well if you want to load the images as background images in the CSS. Check out the second demo to see a version that loads the images as background elements in CSS.

The difference here is that instead of having the anonymous function add an image element to the page, it adding a div. The loader class that is attached to the DIV uses the high resolution image in the background.

Ok, that is great, but what if you want to load more than one image on the page this way? Take a look at example 3. Here is the modified script at the bottom of the page…

I manually built an array of images. This could be done programmatically, but for simplicity sake, I just made the array. Then the script loops through the array and creates a new image for each element in the array. At this point, the browser will start downloading all three images. Now the anonymous function has a name. This is necessary because of the way scope works in JavaScript. The incrementer is passed in so that each image can be added to the correct element.

Ok, that is cool, but what if you want to make it so that the images don’t start downloading until they are visible on the page. Why make users download images they will not see, if they decide not to scroll down? Also, no point in having a cool fade in effect if no one can see it. Take a look at the fourth and final example file.

The JavaScript here is a bit more complex, so we will build it in pieces…

This event listener in the script at the bottom of the page will keep track of scrolling. The code on line 3 requires some explaining. ScrollTop is tracked differently in Chrome and Firefox (at least at this time). Chrome needs body.scrollTop, Firefox needs documentElement.scrollTop. Now it is pretty unusual to see an assignment statement like this, with the || or in it. Here is how it works: Depending on if you are in Chrome (webkit based browser) or Firefox (Mozilla based browser) one of those two statements will always be zero. JavaScript, when assigning a value to pageTop, will assign the one that has a value, since the other is essentially false. Weird, I know, but it works. Check it out in the console log.

Add these two variables at the top of the script, then, in the script add an if statement that checks the position of offsetTop as we scroll for each article. The counter only gets incremented once we get past the top of the article. When we get to the bottom of the page, the counter increments to 3, which is beyond our article array, and so we start getting an error. Notice, when you run this in the Chrome console, the counter gets checked hundreds of times for each article. That poses a challenge.

Let’s deal with the error we get with the counter when we scroll past the top of the third article first.

Adding a variable to keep track of how many articles we have, and putting an IF statement in that only increments the counter if the counter is one less than the number of articles will do the trick. No more errors at the end of the page.

Now here is the tricky problem. I want to trigger loading each image once, based on the position of the scroll.

If I could set my loader to run when a certain pixel is hit during scrolling, that would work great. However, scrolling is somewhat unpredictable and I can be certain I will hit that specific pixel, especially if I am scrolling fast. If I set it for a range of pixels, then the loader will run multiple times while I am scrolling in that range, and I only want it to fire ONE time for each image as it scrolls onto the page.

Here is the solution I came up with…

At the top of the script, with the other variables, make a new one called loaded and make it an empty array. Then I used a for loop to push a zero into the array for each article on the page.

Then add one more IF statement to the event listener that will check to see if the value in the array is set to zero. If it is, increment it to 1. This way, the array will only be changed ONCE for each article, which is exactly what we need for our script. This is the hardest part. The rest is pretty much what we implemented in example 3.

On line 6 I added the array for the photos back in. On line 30 put in the bit that actually loads the image when it gets to the top of the page. And finally, the imageLoad function is added at the bottom of the script. It has one additional argument, so that we can know which we want to load as the page scrolls.

The script is working well, except that the first image never loads. That is because it is already on the page when it loads and the user never scrolls to get to it. Also, I want the other images to load as they come into view, not when they get to the top of the page. Below is the slightly modified code and the final script.

Line 9 gets the height of the user’s window and subtracts 200 pixels from it. This variable is then used on line 27 making it so that the image will fade in once it is 200 pixels up from the bottom of the user’s window.

To get the first image to load, I just added the original loading script on lines 16, 17 & 18. This is an exact copy of the code from lines 38, 39 & 40, but it will only load that first image.

That is it! You can download the files and play around with them. There are lots of ways this script can be modified to suit your specific needs. Have fun with it!

Leave a Reply


Featured Projects

  1. ZAP Creative

    Responsive Design, HTML, CSS, JS, PHP and WordPress

  2. Animal Shelter

    Responsive Design, HTML, CSS, JS, PHP and MySQL

  3. Experimental JavaScript

    Responsive Design, HTML, CSS, and JavaScript


    HTML, CSS & JavaScript focusing on jQuery plugins

  5. Syllabus Generator

    PHP & MySQL Web Application

  6. CodeSnap Web Application

    PHP & MySQL web application

More Projects

  1. 4.23 Microsite

    Experimental JavaScript Microsite

  2. Audio Space

    HTML, CSS & jQuery

  3. Bttrfly Productions

    HTML, CSS, JS, & WordPress

  4. Desktop Repainter

    HTML, CSS & JS

  5. Bluephant Dental

    HTML, CSS, & JS

  6. Zen Music Festival

    HTML & CSS

About William Mead…

William Mead Photo

This site shows some of my front-end design and development work and shows how I use these projects to teach students about those same topics.

Find me on these networks

Professional Qualities

  • Enthusiastic about teaching, learning, managing projects and building products for the web.
  • Deeply creative and always playful. Frequently engrossed in solving complex puzzles.
  • Thoroughly engaged in the design and web development industries.
  • A visual learner with an analytical mind.
  • Introspective and always striving for improvement.


I am particularly good at bending CSS to do my bidding, and I really enjoy creating custom interactivity with JavaScript. I am always excited about new problems that need to be tackled.

Contact Me…

I look forward to hearing from you. Please contact me using the form or directly by email or phone.


Your Name (required)

Your Email (required)


Your Message

Anti Spambot Question