question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation on scrollObservable

See original GitHub issue

I wanted throw this issue up incase others have struggled to work with scrollObservable. For my particular use case, I had a reusable component that I was using in many different situations in the app. At times, I wanted to load the thumbnail of the card on scroll, but others times I wanted to load in a horizontal slider.

Furthermore, the thumbnail just showed the first image of a slideshow of images that could be paged through on the card. I wanted each image in the slideshow to lazy load as well.

I wanted to share some of the code I used to accomplish this in hopes that it could help someone else.

Setup in ngOnInit

this.loadObservable = new Subject<string>();
const targetEl = this.scroller.scrollContainers.entries().next().value[0].getElementRef();  // I have only 1 scrolling container in my app. 
this.scrollTargetObservable  = fromEvent(targetEl.nativeElement, 'scroll');
this.loadImage = combineLatest(this.scrollTargetObservable, this.loadObservable.pipe(startWith('scroll')));

Template:

<div class="image-overlay" #imageOverlay (click)="itemSelected.emit()" [defaultImage]="'https://<image-cdn>.net/'+images[startingIndex]+'?auto=format&w=' + width + '&h=auto&q=1&blur=100'" [lazyLoad]="'https://<image-cdn>.net/'+images[startingIndex]+'?auto=format&w=' + width + '&h=auto&q=50'" (swipeleft)="next($event, 'swipe')" (swiperight)="prev($event, 'swipe')" [scrollObservable]="loadImage"></div>

Call this method whenever I need to load the thumbnail, or load the active image in my slideshow.

  loadThumb() {
    setTimeout(() => {
      this.loadObservable.next('scroll');
      this.cd.detectChanges();
    }, 0);
  }

You might not need to wrap yours in a setTimeout or detectChanges…depending on your use case.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
lalitmeecommented, Aug 16, 2018

thank you, guys. I solved my problem by using offset 😃

1reaction
calbear47commented, Aug 16, 2018

this.scoller is an instance of the scroller service provided by Angular Material. You can get it from DI just like any other service.

Scroller API Docs

Read more comments on GitHub >

github_iconTop Results From Across the Web

Intersection Observer API - MDN Web Docs
Implementing "infinite scrolling" web sites, where more and more content is loaded and rendered as you scroll, so that the user doesn't have...
Read more >
Scrolling (complete) / Interactive Info Vis - Observable
Below is a simple scroller that changes the color of the background when scrolled. The objective is to: Place a simple chart in...
Read more >
Scroll Events and Intersection Observer - Beginner JavaScript
In this video we will learn about scroll events. A scroll event is when someone goes ahead and scrolls on the page or...
Read more >
Introduction to scroll animations with Intersection Observer
The Intersection Observer (IO) detects when an element enters or leaves the viewport (or a parent element). It can be used to easily...
Read more >
Intersection observer and infinite scroll in a visual way
To see if the user is at the bottom, we listen to scroll events in the document, and then we run a function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found