Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define with customElements.define() #9

Merged
merged 6 commits into from Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions app.js
@@ -0,0 +1,5 @@
import LazyloadImage from './lazyload-image.js';

customElements.define('lazyload-image', LazyloadImage, {
extends: 'img'
});
54 changes: 30 additions & 24 deletions index.html
Expand Up @@ -5,7 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>lazyload-image</title> <title>lazyload-image</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/9.3.0/styles/github.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/9.3.0/styles/github.min.css">
<link rel="import" href="lazyload-image.html"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,700">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto Mono:400,700">
<style> <style>
body { font-family: Roboto, Helvetica, Arial, sans-serif; } body { font-family: Roboto, Helvetica, Arial, sans-serif; }
code { font-family: "Roboto Mono", "Source Code Pro", Consolas, "Courier New", monospace; } code { font-family: "Roboto Mono", "Source Code Pro", Consolas, "Courier New", monospace; }
Expand Down Expand Up @@ -38,41 +39,46 @@ <h2>Install</h2>
# via bower # via bower
$ bower install lazyload-image</code></pre> $ bower install lazyload-image</code></pre>
<h2>Usage</h2> <h2>Usage</h2>
<p>Load <code class="html">lazyload-image.html</code> in your HTML.</p> <p>Load <code class="html">lazyload-image.js</code> in your JavaScript.</p>
<pre><code class="html">&lt;link rel="import" href="lazyload-image.html"&gt;</code></pre> <pre><code class="javascript">import LazyloadImage from './lazyload-image.js';

customElements.define('lazyload-image', LazyloadImage, {
extends: 'img'
});</code></pre>
<p>Modify your <code class="html">&lt;img&gt;</code> elements such as following.</p> <p>Modify your <code class="html">&lt;img&gt;</code> elements such as following.</p>
<pre><code class="html">&lt;img is="lazyload-image" src="path/to/your/image.jpg" width="100" height="100"&gt;</code></pre> <pre><code class="html">&lt;img is="lazyload-image" src="path/to/your/image.jpg" width="100" height="100"&gt;</code></pre>
<p>You can specify load offset.</p> <p>You can specify load offset.</p>
<pre><code class="html">&lt;img is="lazyload-image" src="path/to/your/image.jpg" offset="200" width="100" height="100"&gt;</code></pre> <pre><code class="html">&lt;img is="lazyload-image" src="path/to/your/image.jpg" offset="200" width="100" height="100"&gt;</code></pre>
<h2>Fallback</h2> <h2>Fallback</h2>
<p>If browser does not support <code class="javascript">document.registerElement()</code>, Images will be loaded as usual.</p> <p>If browser does not support <code class="javascript">customElements.define()</code>, Images will be loaded as usual.</p>
<h2>Demo</h2> <h2>Demo</h2>
<p>Recommend you to open <strong>DevTools's Elements tab</strong> to see behavior of <code class="html">&lt;lazyload-image&gt;</code>.</p> <p>Recommend you to open <strong>DevTools's Elements tab</strong> to see behavior of <code class="html">&lt;lazyload-image&gt;</code>.</p>
<div class="image-container"> <div class="image-container">
<img is="lazyload-image" offset="200" src="assets/img/1.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/1.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/2.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/2.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/3.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/3.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/4.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/4.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/5.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/5.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/6.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/6.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/7.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/7.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/8.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/8.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/9.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/9.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/10.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/10.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/11.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/11.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/12.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/12.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/13.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/13.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/14.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/14.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/15.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/15.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/16.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/16.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/17.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/17.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/18.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/18.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/19.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/19.jpg" width="100%">
<img is="lazyload-image" offset="200" src="assets/img/20.jpg" width="100%"> <img is="lazyload-image" offset="200px" src="assets/img/20.jpg" width="100%">
</div> </div>
</div> </div>
<a href="https://github.com/1000ch/lazyload-image" class="github-corner"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style> <a href="https://github.com/1000ch/lazyload-image" class="github-corner"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<script src="https://cdn.jsdelivr.net/highlight.js/9.3.0/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/highlight.js/9.3.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script> <script>hljs.initHighlightingOnLoad();</script>
<script type="module" src="app.js"></script>
</body> </body>
</html> </html>
56 changes: 0 additions & 56 deletions lazyload-image.html

This file was deleted.

84 changes: 84 additions & 0 deletions lazyload-image.js
@@ -0,0 +1,84 @@
export default class LazyloadImage extends HTMLImageElement {
static get FALLBACK_IMAGE() {
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAEElEQVR42gEFAPr/AP///wAI/AL+Sr4t6gAAAABJRU5ErkJggg==';
}

static get observedAttributes() {
return [
'offset'
];
}

constructor(width, height) {
super(width, height);

this.original = this.currentSrc || this.src;
this.src = LazyloadImage.FALLBACK_IMAGE;
this.onIntersect = this.onIntersect.bind(this);
}

connectedCallback() {
this.observe();
}

disconnectedCallback() {
this.unobserve();
}

get offset() {
return this.getAttribute('offset');
}

set offset(value) {
this.setAttribute('offset', value);
}

get observer() {
if (!this.intersectionObserver) {
this.intersectionObserver = new IntersectionObserver(this.onIntersect, {
rootMargin: this.offset
});
}

return this.intersectionObserver;
}

observe() {
this.observer.observe(this);
}

unobserve() {
this.observer.unobserve(this);
this.observer.disconnect();
}

onIntersect(entries) {
if (entries.length === 0) {
return;
}

if (entries[0].intersectionRatio <= 0) {
return;
}

this.addEventListener('load', () => {
this.unobserve();
});

this.addEventListener('error', () => {
this.src = LazyloadImage.FALLBACK_IMAGE;
this.unobserve();
});

this.src = this.original;
}

attributeChangedCallback(name, oldValue, newValue) {
if (this.observer === null) {
return;
}

this.unobserve();
this.observe();
}
}
19 changes: 14 additions & 5 deletions readme.md
Expand Up @@ -20,21 +20,30 @@ $ bower install lazyload-image


## Usage ## Usage


Load `lazyload-image.html` in your HTML. Load `lazyload-image.js` in your JavaScript.


```html ```javascript
<link rel="import" href="lazyload-image.html"> import LazyloadImage from './lazyload-image.js';

customElements.define('lazyload-image', LazyloadImage, {
extends: 'img'
});
``` ```


Modify your `<img>` elements such as following. Modify your `<img>` elements such as following.


```html ```html
<img is="lazyload-image" src="path/to/your/image.jpg" offset="200" width="100" height="100"> <img
is="lazyload-image"
src="path/to/your/image.jpg"
offset="200px"
width="100"
height="100">
``` ```


## Fallback ## Fallback


If a browser does not support `document.registerElement()`, images will be loaded as usual. If a browser does not support `customElements.define()`, images will be loaded as usual.


## License ## License


Expand Down