diff --git a/frontend_v2/src/app/Directives/image.lazyload.ts b/frontend_v2/src/app/Directives/image.lazyload.ts new file mode 100644 index 0000000000..89f9516c11 --- /dev/null +++ b/frontend_v2/src/app/Directives/image.lazyload.ts @@ -0,0 +1,31 @@ +import { AfterViewInit, Directive, ElementRef, HostBinding, Input } from '@angular/core'; + +@Directive({ + selector: 'img[appLazyLoadImage]' +}) + +export class LazyImageDirective implements AfterViewInit { + + @HostBinding('attr.src') srcAttribute = null; + + @Input() src: string; + + constructor(private el: ElementRef) {} + + ngAfterViewInit() { + if(window && 'IntersectionObserver' in window) { + const obs = new IntersectionObserver(entries => { + entries.forEach(({ isIntersecting }) => { + if (isIntersecting) { + this.srcAttribute = this.src; + obs.unobserve(this.el.nativeElement); + } + }); + }); + obs.observe(this.el.nativeElement); + } + else { + this.srcAttribute = this.src; + } + } +} diff --git a/frontend_v2/src/app/components/publiclists/challengelist/challengecard/challengecard.component.html b/frontend_v2/src/app/components/publiclists/challengelist/challengecard/challengecard.component.html index 489b284970..4e3743d5b4 100644 --- a/frontend_v2/src/app/components/publiclists/challengelist/challengecard/challengecard.component.html +++ b/frontend_v2/src/app/components/publiclists/challengelist/challengecard/challengecard.component.html @@ -1,6 +1,6 @@
{{ challenge['title'] }}
diff --git a/frontend_v2/src/app/shared/shared.module.ts b/frontend_v2/src/app/shared/shared.module.ts index 654e30d00e..62e0739ff6 100644 --- a/frontend_v2/src/app/shared/shared.module.ts +++ b/frontend_v2/src/app/shared/shared.module.ts @@ -6,6 +6,7 @@ import { FormsModule } from '@angular/forms'; // import directive import { PasswordMismatchValidatorDirective } from '../Directives/password.validator'; import { EmailValidatorDirective } from '../Directives/email.validator'; +import { LazyImageDirective } from '../Directives/image.lazyload'; // import component import { HeaderStaticComponent } from '../components/nav/header-static/header-static.component'; @@ -19,6 +20,7 @@ import { UtilityModule } from '../components/utility/utility.module'; declarations: [ PasswordMismatchValidatorDirective, EmailValidatorDirective, + LazyImageDirective, HeaderStaticComponent, FooterComponent, NotFoundComponent, @@ -27,6 +29,7 @@ import { UtilityModule } from '../components/utility/utility.module'; exports: [ PasswordMismatchValidatorDirective, EmailValidatorDirective, + LazyImageDirective, CommonModule, RouterModule, FormsModule,