Skip to content

Commit

Permalink
Define with customElements.define()
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Sep 17, 2016
1 parent f5fa7e1 commit c0209a9
Showing 1 changed file with 49 additions and 29 deletions.
78 changes: 49 additions & 29 deletions lazyload-image.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,38 @@
* Copyright 1000ch
* licensed under the MIT license.
*/
window.LazyloadImage = (function () {
'use strict';
class LazyloadImage extends Image {
constructor(width, height) {
super(width, height);

const FALLBACK_IMAGE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAEElEQVR42gEFAPr/AP///wAI/AL+Sr4t6gAAAABJRU5ErkJggg==';
const LazyloadImagePrototype = Object.create(HTMLImageElement.prototype);
const DEFAULT_OFFSET = 0;
this.original = this.currentSrc || this.src;
this.src = LazyloadImage.FALLBACK_IMAGE;
this.offset = Number(this.getAttribute('offset')) || 0;
this.visibleChanged = this.visibleChanged.bind(this);
this.observer = new IntersectionObserver(this.visibleChanged, {
rootMargin: `${this.offset}px`
});
}

static get FALLBACK_IMAGE() {
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAEElEQVR42gEFAPr/AP///wAI/AL+Sr4t6gAAAABJRU5ErkJggg==';
}

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

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

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

LazyloadImagePrototype.visibleChanged = function(changes) {
visibleChanged(changes) {
for (let change of changes) {
let intersectionRect = change.intersectionRect;
if (intersectionRect.height * intersectionRect.width > 0) {
Expand All @@ -23,34 +47,30 @@
});
this.addEventListener('error', () => {
this.removeAttribute('srcset');
this.src = FALLBACK_IMAGE;
this.src = LazyloadImage.FALLBACK_IMAGE;
this.observer.unobserve(this);
});
this.src = this.original;
break;
}
}
};
}

LazyloadImagePrototype.createdCallback = function() {
this.original = this.currentSrc || this.src;
this.src = FALLBACK_IMAGE;
this.offset = Number(this.getAttribute('offset')) || DEFAULT_OFFSET;
this.observer = new IntersectionObserver(this.visibleChanged.bind(this), {
rootMargin: `${this.offset}px`
});
};

LazyloadImagePrototype.attachedCallback = function() {
this.observer.observe(this);
};

LazyloadImagePrototype.detachedCallback = function() {
this.observer.unobserve(this);
};
attributeChangedCallback(name, oldValue, newValue) {
switch (name) {
case 'offset':
this.offset = newValue;
this.observer.unobserve(this);
this.observer = new IntersectionObserver(this.visibleChanged, {
rootMargin: `${this.offset}px`
});
this.observer.observe(this);
break;
}
}
}

return document.registerElement('lazyload-image', {
prototype: LazyloadImagePrototype,
extends: 'img'
});
})();
customElements.define('lazyload-image', LazyloadImage, {
extends: 'img'
});
</script>

0 comments on commit c0209a9

Please sign in to comment.