Fireship · @fireship
Lazy-Loaded Images - Code This, Not That
Uploaded January 25, 2019 · Length 4:47
Summary
Lazy-loaded images using vanilla JavaScript and Intersection Observer to improve performance and reduce unnecessary calculations.
- lazy-loaded images
- vanilla javascript
- intersection observer
- performance optimization
- dom manipulation
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
[Music]
- 0:08
welcome to another episode of Code This
- 0:09
Not That where we try to write the best
- 0:11
code possible and avoid the bad stuff
- 0:13
today we're going to look at
- 0:13
intersection Observer and how to
- 0:15
determine the visibility of elements in
- 0:17
the Dom this is a very useful tool
- 0:18
because you can use it for things like
- 0:20
infinite scroll lazy loaded images and
- 0:22
animations in today's video we'll look
- 0:24
at two different ways we can build out
- 0:25
this lazy loaded image feature using
- 0:27
nothing but vanilla JavaScript if you're
- 0:28
new here like And subscribe and grab the
- 0:30
source code from fireship IO and I'll be
- 0:32
giving away the first ever fireship IO
- 0:34
t-shirt all you have to do is leave a
- 0:35
comment below and we'll pick a random
- 0:37
winner next week intersection Observer
- 0:39
is a browser API that we can use to
- 0:41
observe the visibility of an element in
- 0:42
the viewport it's currently supported by
- 0:44
most browsers with the main exception
- 0:46
being Safari but there is a polyfill
- 0:48
that you can use as needed I actually
- 0:49
use it on fireship iO to Lazy load the
- 0:51
comment section the comments live in
- 0:53
fire store so this prevents unnecessary
- 0:55
reads to the database and another thing
- 0:57
you might notice on Fire Ship is that
- 0:59
page transitions are are super fast this
- 1:01
is made possible in part by a library
- 1:02
called quicklink which uses intersection
- 1:04
Observer to preload all the links that
- 1:06
are visible on the page so there's all
- 1:08
kinds of cool use cases but let's go
- 1:10
ahead and get started by writing some
- 1:11
bad code what we have here is just a
- 1:13
basic HTML page and we have some high
- 1:15
resolution images on it that we want to
- 1:17
Lazy load so when the user Scrolls to an
- 1:19
image it will load it and then do this
- 1:21
little CSS animation the CSS itself is
- 1:23
pretty straightforward we start with an
- 1:25
opacity of zero in a transform and then
- 1:27
we fade in by adding this fade class
- 1:29
which sets the OPAC capacity to one then
- 1:31
in the HTML we'll go ahead and reference
- 1:33
a script called appjs and that'll be a
- 1:35
deferred script then you'll notice with
- 1:37
the images in this page that instead of
- 1:38
having a source attribute they have a
- 1:40
data lazy attribute this is our own
- 1:42
custom attribute that we can use to
- 1:43
point to the image itself and then we'll
- 1:45
use our JavaScript to set it as the
- 1:47
actual image source when it becomes
- 1:48
visible in the viewport the first thing
- 1:50
we'll do is grab all of the images in
- 1:52
the Dom by using query selector all then
- 1:54
we'll listen to The Scroll event on the
- 1:56
window and every time the user Scrolls
- 1:58
we'll be able to recalculate whether not


