Display a Loading icon while images loads

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Display a Loading icon while images loads



I want to show a background image/loading spinner inside a div that will load an image inside of it, the image will show once it's fully loaded doing something like this:


div


<div style="background-image:url('imageThatWillAppearBeforeLoad')"></div>



Demo (In jQuery)



How can I have the same using Angular2/Ionic2?





Possible duplicate of Ionic 2 : (Lazy) Loading spinner for pictures
– Guillaume Le Mière
Sep 2 '16 at 23:32




3 Answers
3



Create a component that shows the placeholder image until the requested image is loaded, and hides the requested image. Once the image is loaded, you hide the placeholder and show the image.


@Component(
selector: 'image-loader',
template: `<img *ngIf="!loaded" src="url-to-your-placeholder"/>
<img [hidden]="!loaded" (load)="loaded = true" [src]="src"/>`
)
export class ImageLoader
@Input() src;



See it working in Plunker.



Update



Now that I understand the requirements better, here's a solution with background image. It's a little hacky, and I like the original one better...


@Directive(
selector: '[imageLoader]'
)
export class ImageLoader
@Input() imageLoader;

constructor(private el:ElementRef)
this.el = el.nativeElement;
this.el.style.backgroundImage = "url(http://smallenvelop.com/demo/image-loading/spinner.gif)";


ngOnInit()
let image = new Image();
image.addEventListener('load', () =>
this.el.style.backgroundImage = `url($this.imageLoader)`;
);
image.src = this.imageLoader;




Updated plunker.





Your code is working fine, but for my case as I've posted, I've a <div style="background-image:url('imageThatWillAppearBeforeLoad')"></div> that contains an image in background, I've tried to tweak your code to match the div I have but It didn't worked, Is it possible to make it work as a div ?
– Folky.H
Jul 24 '16 at 13:19


<div style="background-image:url('imageThatWillAppearBeforeLoad')"></div>


div


div





For what I achieved so far I guess that (load) does not work for some reason! Because I've replaced the img with div, etc... when I go to check the DOM, I see hidden which means that loaded = true was not applied for some reason, what makes me even sure that there is an issue with (load) is that I replace it with (click) then I place it in the placeholder div and it worked! So, do you have any idea why (load) didn't worked?
– Folky.H
Jul 24 '16 at 13:33


img


div


DOM


(load)


(click)


div


(load)





It didn't work because divs don't have an onload event... This is why I made the component that way.
– yarons
Jul 24 '16 at 13:34


div


onload





Does it have to be a div? Do you need the requested image also as a background-image, or can that be a img element?
– yarons
Jul 24 '16 at 13:36



div


img





It needs to be a div not img
– Folky.H
Jul 24 '16 at 13:39


div


img



Here is a copy of one of my posts



I finally solved that problem with CSS! When an image is loading in ionic 2, the ion-img tag doesn't have any class. However, when the image is finally loaded, the ion-img tag get the class "img-loaded".



Here is my solution :


<ion-img [src]="img.src"></ion-img>
<ion-spinner></ion-spinner>



And my CSS :


.img-loaded + ion-spinner
display:none;



Simply use this function


loadImage(element: HTMLElement, imagePath: string, spinnerPath: string)
element.tagName === 'img'
? element.setAttribute('src', spinnerPath)
: (element.style.background = `url($spinnerPath) 50% 50% no-repeat`);
const image = new Image();
image.crossOrigin = 'Anonymous';
image.src = imagePath;
image.onload = () =>
element.tagName === 'img'
? element.setAttribute('src', imagePath)
: (element.style.background = `url($imagePath)`);
;



You don't have to worry about your element is image or div or whatever it is?



example if you didn't figure out how to use it


ngOnInit()
const myElement = document.getElementsByClassName('my-element')[0];
// or however you want to select it.
const imagePath = 'path/to/image.png';
const spinnerPath = 'path/to/spinner.gif'; // or base64 spinner url.

this.loadImage(myElement, imagePath, spinnerPath);



Happy coding 😉






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard