2

I Angular have a grid with 1000+ backgroundimages. I want to lazy load them on scroll. This is my code:

<div inViewport [inViewportOptions]="{ threshold: [0] }" (inViewportAction)="onIntersection($event)" class="artist_image" [ngStyle]="{ 'background-image': 'url(\''+ artist.image + '\')'}"></div>
 public onIntersection({
    target,
    visible,
  }: {
    target: Element;
    visible: boolean;
  }): void {
    this.renderer.addClass(target, visible ? "active" : "inactive");
    this.renderer.removeClass(target, visible ? "inactive" : "active");
  }
.artist_image
    height: 0
    padding-bottom: 100%
    background-size: cover
    background-position: center
    background-repeat: no-repeat
    &.inactive
        background-image: none !important

the active class gets fired perfectly. But all the background images are still being loaded in the background. Even if they are not displayed.

Any ideas?

2
  • I just tried it with simple list and it worked for me. Can you please reproduce it in stackblitz? Commented Dec 20, 2021 at 5:46
  • I used the package of @Dev Gourav and now it works like a charm. Commented Dec 20, 2021 at 5:55

1 Answer 1

2

Here's a npm package that solves the same issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.