• Stars
    star
    213
  • Rank 185,410 (Top 4 %)
  • Language
    HTML
  • License
    MIT License
  • Created almost 8 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A custom image element that lets you load resources on demand

lazy-image

<lazy-image> is a custom element that contains an image that has the option to be loaded only on-demand, for performance reasons. If a <lazy-image> is inactive, then its source is not loaded (the XHR won't be made until the active attribute is set on the element).

<lazy-image> has 3 attributes:

  • src the image source (like you would use with a <img>
  • alt, the alternate text for the image the image source (like you would use with a <img>)
  • active, whether the image should be loaded or not

How to use

You can install the element with bower (npm support coming soon, hold tight)

bower install notwaldorf/lazy-image

Then you can drop a <script src="bower_components/lazy-image.js"></script> in the page where you want to use it. Custom elements/Shadow DOM are not yet supported in all browsers, so we recommend the webcomponentsjs polyfills. In my demo I use the webcomponents-sd-ce.js bundle (because I am not using any HTML Imports and I didn't bother with IE11), but for a fancier way to load the polyfills, check the webcomponents-loader and their docs.

On-demand loading

For a <lazy-image> to load, it must have the active property set to true. In the example below, the images will only load when clicked (you can check the network tab in your favourite developer tools to see that there's no initial request for these files). This happens because they each start off with the active property set to false, and have a click event listener, that sets the <lazy-image>'s active attribute to true:

<lazy-image src="..." alt="..." id="i"></lazy-image>

<script>
  i.addEventListener('click', function() {
    if (!this.active)
      this.active = true;
  });
</script>

If you want to have some global setting that controls all <lazy-images> on the page (i.e. activates or deactivates all of them), you can set the window.LazyImageSiteDefaultActive global before loading the lazy-image.js script.

Intersection Observer

Intersection observers let you figure out when an element enters into view. Combined with a <lazy-image>, this lets you only load images that are scrolled into view, while leaving images at the bottom of the page that haven't been seen inactive.

// Create an observer.
var observer = new IntersectionObserver(onChange, {
  threshold: [0.5]  // rootMargin: '50% 0%'
});

// That observes all the random images we've created.
els.forEach(el => observer.observe(el));

// Whenever we scroll...
function onChange(changes) {
  changes.forEach(change => {
    var el = change.target;
    if (!el.active)
      el.active = true;
    observer.unobserve(el);  // Don't care anymore.
  });
}

๐Ÿ˜˜, monica

More Repositories

1

tiny-care-terminal

๐Ÿ’–๐Ÿ’ป A little dashboard that tries to take care of you when you're using your terminal.
JavaScript
5,963
star
2

emoji-translate

๐Ÿ“š Translate text to โœจemoji โœจ!
JavaScript
1,202
star
3

font-style-matcher

โšก๏ธ๐ŸŽจ Matches the x-heights and widths of two fonts
JavaScript
509
star
4

github-canned-responses

๐Ÿ’ค (inactive) Because copy pasting isn't that great.
JavaScript
369
star
5

emojillate

๐Ÿ“ท๐ŸŽจ Pixellate an image into emoji
HTML
342
star
6

frankly

๐Ÿ”ง A Polymer dashboard of issues/PRs for any number of GitHub repos
HTML
321
star
7

emoji-rain

โ˜”๏ธโœจ A Polymer element that makes it rain emoji
HTML
217
star
8

cat-dns

๐Ÿ’ป๐Ÿฑ A DNS server that resolves everything to cats.
JavaScript
203
star
9

picasso

๐ŸŽจ๐Ÿ—“ [DEPRECATED] A pretty Google Calendar theme
CSS
159
star
10

caturday-post

๐Ÿฑ๐Ÿ“ฒ Push notifications with cats and Polymer
HTML
139
star
11

flash-cards

๐Ÿ““๐Ÿ™ŠA simple flash cards app to help you learn Japanese (and other languages!)
JavaScript
137
star
12

og-emoji-font

โœจ๐Ÿ“ Emoji font based on the original DoCoMo emoji set
130
star
13

.not-quite-dotfiles

๐Ÿ’ฐ NO JUDGING
Shell
122
star
14

dear-sir-or-madam

๐Ÿ’Œ Bookmarklet that ransomifies your internets
JavaScript
103
star
15

literally

๐ŸŽž๐ŸŽ€ Literally another slide making thingie.
HTML
82
star
16

mojibrag

๐Ÿ“ข stuff. Use โœจ๐Ÿ™Š๐Ÿ˜‚๐Ÿ”ฅ.
HTML
74
star
17

doodle

๐ŸŽจ๐Ÿ‘พ Doodle with pixels and without pressure
JavaScript
68
star
18

midi-visualizer

A midi visualizer web component
JavaScript
56
star
19

is-mu-an-asleep

Electron app to find out if @muan is asleep
JavaScript
52
star
20

twemojify

๐Ÿ’ฅ Use Twitter's emoji api to fix emoji in Chrome
JavaScript
52
star
21

emoji-selector

โœ๏ธโœจ A Polymer paper-input-addon element that adds an emoji keyboard inputs
HTML
51
star
22

knit-a-stitch

๐ŸŽ€ Draw a pattern that you want to knit, and get the knitting chart for it!
JavaScript
48
star
23

diagramophone

๐ŸŽจ code your diagrams, because drawing them really sucks
JavaScript
39
star
24

muster

โ‡๏ธ A website that lets you play with overlapping patterns.
HTML
31
star
25

notwaldorf.github.com

๐Ÿ“ข I lost a bet and now I have a blog.
HTML
31
star
26

to_emoji

๐Ÿค–โœจ Twitter bot that translates every tweet you send it to emoji
JavaScript
30
star
27

ruby-eh

๐Ÿ’Ž Ruby for Canadians: an instruction manual.
Ruby
25
star
28

sparkle

โœจPixelate and sparkle an image. Any image.
JavaScript
22
star
29

example-magenta-in-ts

A demo of using @magenta/music as a dev-dependency in a TypeScript project
TypeScript
21
star
30

polymer-color-picker

[๐Ÿ’ค Deprecated: Go to https://github.com/polymerelements/paper-swatch-picker instead!] A color picker with all the Material Design colours
HTML
18
star
31

is-ie10-dead

๐Ÿ’€ Well, is it?
HTML
17
star
32

meowto

๐Ÿ”ฎ A client-side URL shortener
JavaScript
17
star
33

tensorflow-experiments

JavaScript
13
star
34

gh-wiki-to-pages

[WIP] Automatically publish a GitHub wiki to a gh-pages Jekyll site
CSS
13
star
35

polymer-gold-elements-demo

๐Ÿ’ธ A checkout form using the Polymer gold elements
HTML
10
star
36

old-research-papers

Old Reinforcement Learning research from university
9
star
37

polymer-demo-transmogrifier

[๐Ÿ’ค Deprecated: Go to https://github.com/polymerelements/iron-demo-helpers instead!] Polymer element that renders both the source and demo of a code snippet
HTML
9
star
38

emoji-web-components-art-talk

I gave a talk about web components and art and emoji and this is it.
HTML
7
star
39

bear-or-berra

[WIP, don't look] Who said it, Yogi Bear or Yogi Berra?
JavaScript
7
star
40

shrinkwrap

๐Ÿ‘” [forever unfinished] t-shirt sizing / progress tracking app for scrum planning. because sometimes you just have to. and because i have to learn websockets somehow.
JavaScript
7
star
41

fierimas

๐ŸŽ„๐ŸŽ… It's like Guy Fieri's Grocery Games but for holiday presents
JavaScript
4
star
42

fuck-yeah-css

CSS
4
star
43

shpool-stats

๐ŸŽฑ I play pool every day at lunch and stats need to be had.
JavaScript
2
star
44

ponies

๐Ÿด Here, have a pony
1
star
45

better-form

WIP - look away
HTML
1
star
46

hacknight

Montreal all-girl hack night website!
CSS
1
star
47

input-tests

Look away. Playground to test the v1 custom elements polyfill
HTML
1
star
48

hacknight-chrome-extensions

Sample Chrome Extensions for the all-girl hacknight tutorial
JavaScript
1
star