Skip to content

Commit 0701338

Browse files
committed
fix(img): only load ion-img when visible
1 parent 910d56b commit 0701338

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

ionic/components/img/img.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ion-img .img-placeholder {
2020
width: 100%;
2121
height: 100%;
2222

23-
background: #ddd;
23+
background: #eee;
2424

2525
transition: opacity 200ms;
2626
}

ionic/components/img/img.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Component, Input, HostBinding, ViewChild, ElementRef} from 'angular2/core';
22

33
import {isPresent} from '../../util/util';
4+
import {Platform} from '../../platform/platform';
45

56

67
@Component({
@@ -20,7 +21,7 @@ export class Img {
2021
private _h: string;
2122
private _enabled: boolean = true;
2223

23-
constructor(private _elementRef: ElementRef) {}
24+
constructor(private _elementRef: ElementRef, private _platform: Platform) {}
2425

2526
@ViewChild('imgA') private _imgA: ElementRef;
2627
@ViewChild('imgB') private _imgB: ElementRef;
@@ -39,7 +40,7 @@ export class Img {
3940
}
4041

4142
private _update() {
42-
if (this._enabled) {
43+
if (this._enabled && this.isVisible()) {
4344
if (this._useA) {
4445
this._srcA = this._src;
4546

@@ -54,6 +55,11 @@ export class Img {
5455
this._update();
5556
}
5657

58+
isVisible() {
59+
let bounds = this._elementRef.nativeElement.getBoundingClientRect();
60+
return bounds.bottom > 0 && bounds.top < this._platform.height();
61+
}
62+
5763
@HostBinding('class.img-loaded')
5864
private _loaded: boolean = false;
5965

0 commit comments

Comments
 (0)