Fireship · @fireship

Lazy-Loaded Images - Code This, Not That

Uploaded January 25, 2019 · Length 4:47

Lazy-Loaded Images - Code This, Not That

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.

  1. 0:00

    [Music]

  2. 0:08

    welcome to another episode of Code This

  3. 0:09

    Not That where we try to write the best

  4. 0:11

    code possible and avoid the bad stuff

  5. 0:13

    today we're going to look at

  6. 0:13

    intersection Observer and how to

  7. 0:15

    determine the visibility of elements in

  8. 0:17

    the Dom this is a very useful tool

  9. 0:18

    because you can use it for things like

  10. 0:20

    infinite scroll lazy loaded images and

  11. 0:22

    animations in today's video we'll look

  12. 0:24

    at two different ways we can build out

  13. 0:25

    this lazy loaded image feature using

  14. 0:27

    nothing but vanilla JavaScript if you're

  15. 0:28

    new here like And subscribe and grab the

  16. 0:30

    source code from fireship IO and I'll be

  17. 0:32

    giving away the first ever fireship IO

  18. 0:34

    t-shirt all you have to do is leave a

  19. 0:35

    comment below and we'll pick a random

  20. 0:37

    winner next week intersection Observer

  21. 0:39

    is a browser API that we can use to

  22. 0:41

    observe the visibility of an element in

  23. 0:42

    the viewport it's currently supported by

  24. 0:44

    most browsers with the main exception

  25. 0:46

    being Safari but there is a polyfill

  26. 0:48

    that you can use as needed I actually

  27. 0:49

    use it on fireship iO to Lazy load the

  28. 0:51

    comment section the comments live in

  29. 0:53

    fire store so this prevents unnecessary

  30. 0:55

    reads to the database and another thing

  31. 0:57

    you might notice on Fire Ship is that

  32. 0:59

    page transitions are are super fast this

  33. 1:01

    is made possible in part by a library

  34. 1:02

    called quicklink which uses intersection

  35. 1:04

    Observer to preload all the links that

  36. 1:06

    are visible on the page so there's all

  37. 1:08

    kinds of cool use cases but let's go

  38. 1:10

    ahead and get started by writing some

  39. 1:11

    bad code what we have here is just a

  40. 1:13

    basic HTML page and we have some high

  41. 1:15

    resolution images on it that we want to

  42. 1:17

    Lazy load so when the user Scrolls to an

  43. 1:19

    image it will load it and then do this

  44. 1:21

    little CSS animation the CSS itself is

  45. 1:23

    pretty straightforward we start with an

  46. 1:25

    opacity of zero in a transform and then

  47. 1:27

    we fade in by adding this fade class

  48. 1:29

    which sets the OPAC capacity to one then

  49. 1:31

    in the HTML we'll go ahead and reference

  50. 1:33

    a script called appjs and that'll be a

  51. 1:35

    deferred script then you'll notice with

  52. 1:37

    the images in this page that instead of

  53. 1:38

    having a source attribute they have a

  54. 1:40

    data lazy attribute this is our own

  55. 1:42

    custom attribute that we can use to

  56. 1:43

    point to the image itself and then we'll

  57. 1:45

    use our JavaScript to set it as the

  58. 1:47

    actual image source when it becomes

  59. 1:48

    visible in the viewport the first thing

  60. 1:50

    we'll do is grab all of the images in

  61. 1:52

    the Dom by using query selector all then

  62. 1:54

    we'll listen to The Scroll event on the

  63. 1:56

    window and every time the user Scrolls

  64. 1:58

    we'll be able to recalculate whether not