I'm using ng-lazyload-image to lazy load images in my Angular project, i would be able to show a skeleton while the image is loading, i'm yet using ngx-skeleton-loader.
To show the skeleton i tought to check if the image has ng-lazyloaded class in it as it's the only way to know if the image is still loading or not.
My code looks like this:
<img class="image-cover" [defaultImage]="defaultImage" [lazyLoad]="image"/>
<ngx-skeleton-loader #skeleton class="image-cover" [theme]="{ height: '100%', 'margin-bottom': '0' }"></ngx-skeleton-loader>
And i would do something like:
<img #productImage class="image-cover" *ngIf="productImage.classList.contains('ng-lazyloaded'); else skeleton" [defaultImage]="defaultImage" [lazyLoad]="image"/>
<ngx-skeleton-loader #skeleton class="image-cover" [theme]="{ height: '100%', 'margin-bottom': '0' }"></ngx-skeleton-loader>
But how can i check if img has ng-lazyloaded? If i try to use .classList on productImage i just get that it doesn't exists.
PS: the upper code is just like "pseudo-code", it's just an idea.
I've tried even this:
<img #productImage *ngIf="hasLoaded(); else skeleton" class="image-cover"
[defaultImage]="defaultImage" [lazyLoad]="image" />
<ng-template #skeleton>
<ngx-skeleton-loader class="image-cover" [theme]="{ height: '100%', 'margin-bottom': '0' }">
</ngx-skeleton-loader>
</ng-template>
@ViewChild('productImage') productImage: ElementRef;
hasLoaded() {
if (this.productImage?.nativeElement.classList.contains('ng-lazyloaded')) {
return false;
}else {
return true;
}
}
But all skeletons are set to invisible once the first image is loaded.