admin管理员组

文章数量:1391960

with vanilla js, I can write something like this to catch any scroll on any element (pay attention to true as the last argument)

document.addEventListener('scroll', function(e) {
   console.log(e);
}, true);

but with rxjs I can't do the bubbling (or I don't know how) and something like this doesn't work

fromEvent(window, 'scroll').subscribe(console.log);

how to register an event in rxjs that support bubbling?

with vanilla js, I can write something like this to catch any scroll on any element (pay attention to true as the last argument)

document.addEventListener('scroll', function(e) {
   console.log(e);
}, true);

but with rxjs I can't do the bubbling (or I don't know how) and something like this doesn't work

fromEvent(window, 'scroll').subscribe(console.log);

how to register an event in rxjs that support bubbling?

Share Improve this question edited Dec 16, 2020 at 14:07 Mohammad Hossein Amri asked Dec 16, 2020 at 11:02 Mohammad Hossein AmriMohammad Hossein Amri 2,0452 gold badges30 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can pass options to fromEvent (see Examples) as well:

fromEvent(window, 'scroll', { capture: true }).subscribe(console.log);

// or

fromEvent(window, 'scroll', true).subscribe(console.log);

Just in case it might be helpful for someone. To target scroll of specific element:

const el = document.getElementById('element-id-with-scroll') as HTMLElement;

fromEvent(el, 'scroll')
  .pipe(
    debounceTime(300)
  )
 .subscribe(res => console.log(res))

本文标签: javascripthow in rxjs bubble a scroll eventStack Overflow