Sometimes I like to forget that I’m mainly involved in other things and get my hands dirty with low-level stuff: last week I wanted to improve some existing lazy loading code we use with JavaScript.
Let’s suppose that we have a page showing almost 100 images, 500kb per image, 5MB of a webpage.
It’s clearly unpractical to load all the images at once, since you would force the use to download stuff that he would only see scrolling down with the mouse, so the solution would be to store all the images’ path in an HTML attribute and trigger the load just for visible images in the current window.
You’d have an HTML like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
and the lazy laoding function looks like:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
As you see, we only trigger lazy loading for visible
items, which are appearing in the current window:
given the windowHeight
, we calculate visibility
based on the difference between the item and
the mouse scroll, so that we can see whether the
product is comprehended in the current window
or not.
To trigger lazy loading you need to listen for
mouse scroll events and domready
( if some
images would be visible without scrolling ):
1 2 3 4 5 6 7 |
|
We, at Namshi, use the same approach for catalog listing pages.