3
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
Code distributed by Google as part of the polymer project is also
8
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11
<link rel="import" href="../polymer/polymer.html">
12
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
15
`iron-image` is an element for displaying an image that provides useful sizing and
16
preloading options not found on the standard `<img>` tag.
18
The `sizing` option allows the image to be either cropped (`cover`) or
19
letterboxed (`contain`) to fill a fixed user-size placed on the element.
21
The `preload` option prevents the browser from rendering the image until the
22
image is fully loaded. In the interim, either the element's CSS `background-color`
23
can be be used as the placeholder, or the `placeholder` property can be
24
set to a URL (preferably a data-URI, for instant rendering) for an
27
The `fade` option (only valid when `preload` is set) will cause the placeholder
28
image/color to be faded out once the image is rendered.
32
Basically identical to `<img src="...">` tag:
34
<iron-image src="http://lorempixel.com/400/400"></iron-image>
36
Will letterbox the image to fit:
38
<iron-image style="width:400px; height:400px;" sizing="contain"
39
src="http://lorempixel.com/600/400"></iron-image>
41
Will crop the image to fit:
43
<iron-image style="width:400px; height:400px;" sizing="cover"
44
src="http://lorempixel.com/600/400"></iron-image>
46
Will show light-gray background until the image loads:
48
<iron-image style="width:400px; height:400px; background-color: lightgray;"
49
sizing="cover" preload src="http://lorempixel.com/600/400"></iron-image>
51
Will show a base-64 encoded placeholder image until the image loads:
53
<iron-image style="width:400px; height:400px;" placeholder="data:image/gif;base64,..."
54
sizing="cover" preload src="http://lorempixel.com/600/400"></iron-image>
56
Will fade the light-gray background out once the image is loaded:
58
<iron-image style="width:400px; height:400px; background-color: lightgray;"
59
sizing="cover" preload fade src="http://lorempixel.com/600/400"></iron-image>
61
Custom property | Description | Default
62
----------------|-------------|----------
63
`--iron-image-placeholder` | Mixin applied to #placeholder | `{}`
64
`--iron-image-width` | Sets the width of the wrapped image | `auto`
65
`--iron-image-height` | Sets the height of the wrapped image | `auto`
72
<dom-module id="iron-image">
76
display: inline-block;
89
width: var(--iron-image-width, auto);
90
height: var(--iron-image-height, auto);
93
:host([sizing]) #sizedImgDiv {
97
:host([sizing]) #img {
102
@apply(--layout-fit);
104
background-color: inherit;
107
@apply(--iron-image-placeholder);
110
#placeholder.faded-out {
111
transition: opacity 0.5s linear;
116
<div id="sizedImgDiv"
118
hidden$="[[_computeImgDivHidden(sizing)]]"
119
aria-hidden$="[[_computeImgDivARIAHidden(alt)]]"
120
aria-label$="[[_computeImgDivARIALabel(alt, src)]]"></div>
121
<img id="img" alt$="[[alt]]" hidden$="[[_computeImgHidden(sizing)]]">
122
<div id="placeholder"
123
hidden$="[[_computePlaceholderHidden(preload, fade, loading, loaded)]]"
124
class$="[[_computePlaceholderClassName(preload, fade, loading, loaded)]]"></div>
133
* The URL of an image.
136
observer: '_srcChanged',
142
* A short text alternative for the image.
150
* When true, the image is prevented from loading and any placeholder is
151
* shown. This may be useful when a binding to the src property is known to
152
* be invalid, to prevent 404 requests.
157
observer: '_preventLoadChanged'
161
* Sets a sizing option for the image. Valid values are `contain` (full
162
* aspect ratio of the image is contained within the element and
163
* letterboxed) or `cover` (image is cropped in order to fully cover the
164
* bounds of the element), or `null` (default: image takes natural size).
169
reflectToAttribute: true
173
* When a sizing option is used (`cover` or `contain`), this determines
174
* how the image is aligned within the element bounds.
182
* When `true`, any change to the `src` property will cause the `placeholder`
183
* image to be shown until the new image has loaded.
191
* This image will be used as a background/placeholder until the src image has
192
* loaded. Use of a data-URI for placeholder is encouraged for instant rendering.
197
observer: '_placeholderChanged'
201
* When `preload` is true, setting `fade` to true will cause the image to
210
* Read-only value that is true when the image is loaded.
220
* Read-only value that tracks the loading state of the image when the `preload`
231
* Read-only value that indicates that the last set `src` failed to load.
241
* Can be used to set the width of image (e.g. via binding); size may also be
245
observer: '_widthChanged',
251
* Can be used to set the height of image (e.g. via binding); size may also be
259
observer: '_heightChanged',
266
'_transformChanged(sizing, position)'
270
var img = this.$.img;
272
img.onload = function() {
273
if (this.$.img.src !== this._resolveSrc(this.src)) return;
275
this._setLoading(false);
276
this._setLoaded(true);
277
this._setError(false);
280
img.onerror = function() {
281
if (this.$.img.src !== this._resolveSrc(this.src)) return;
285
this._setLoading(false);
286
this._setLoaded(false);
287
this._setError(true);
290
this._resolvedSrc = '';
293
_load: function(src) {
295
this.$.img.src = src;
297
this.$.img.removeAttribute('src');
299
this.$.sizedImgDiv.style.backgroundImage = src ? 'url("' + src + '")' : '';
301
this._setLoading(!!src);
302
this._setLoaded(false);
303
this._setError(false);
307
this.$.img.removeAttribute('src');
308
this.$.sizedImgDiv.style.backgroundImage = '';
310
this._setLoading(false);
311
this._setLoaded(false);
312
this._setError(false);
315
_computePlaceholderHidden: function() {
316
return !this.preload || (!this.fade && !this.loading && this.loaded);
319
_computePlaceholderClassName: function() {
320
return (this.preload && this.fade && !this.loading && this.loaded) ? 'faded-out' : '';
323
_computeImgDivHidden: function() {
327
_computeImgDivARIAHidden: function() {
328
return this.alt === '' ? 'true' : undefined;
331
_computeImgDivARIALabel: function() {
332
if (this.alt !== null) {
336
// Polymer.ResolveUrl.resolveUrl will resolve '' relative to a URL x to
337
// that URL x, but '' is the default for src.
338
if (this.src === '') {
342
var pathComponents = (new URL(this._resolveSrc(this.src))).pathname.split("/");
343
return pathComponents[pathComponents.length - 1];
346
_computeImgHidden: function() {
347
return !!this.sizing;
350
_widthChanged: function() {
351
this.style.width = isNaN(this.width) ? this.width : this.width + 'px';
354
_heightChanged: function() {
355
this.style.height = isNaN(this.height) ? this.height : this.height + 'px';
358
_preventLoadChanged: function() {
359
if (this.preventLoad || this.loaded) return;
362
this._load(this.src);
365
_srcChanged: function(newSrc, oldSrc) {
366
var newResolvedSrc = this._resolveSrc(newSrc);
367
if (newResolvedSrc === this._resolvedSrc) return;
368
this._resolvedSrc = newResolvedSrc;
371
if (!this.preventLoad) {
376
_placeholderChanged: function() {
377
this.$.placeholder.style.backgroundImage =
378
this.placeholder ? 'url("' + this.placeholder + '")' : '';
381
_transformChanged: function() {
382
var sizedImgDivStyle = this.$.sizedImgDiv.style;
383
var placeholderStyle = this.$.placeholder.style;
385
sizedImgDivStyle.backgroundSize =
386
placeholderStyle.backgroundSize =
389
sizedImgDivStyle.backgroundPosition =
390
placeholderStyle.backgroundPosition =
391
this.sizing ? this.position : '';
393
sizedImgDivStyle.backgroundRepeat =
394
placeholderStyle.backgroundRepeat =
395
this.sizing ? 'no-repeat' : '';
398
_resolveSrc: function(testSrc) {
399
return Polymer.ResolveUrl.resolveUrl(testSrc, this.ownerDocument.baseURI);