javascript - How to listen scroll event for x scroll and y scroll Individually - Stack Overflow

I want to listen scroll event and handler them by two function for two scroll.One need a throttleTime(

I want to listen scroll event and handler them by two function for two scroll.

One need a throttleTime() to save resource and the other cannot use throttleTime() because it need trigger immediately and it doesn't cast too much.

Now I add two fromEvent to handle them but question is they will both be fired if scroll event is triggered.

fromEvent(this.vscroll.nativeElement, 'scroll').pipe(throttleTime(200)).subscribe((e) => {
  if (this.timer) {
    clearTimeout(this.timer);
    this.timer = null;
    this.timer = setTimeout(() => {
      this.updateShow();
    }, 201);
  } else {
    this.timer = setTimeout(() => {
      this.updateShow();
    }, 201);
  }
});
fromEvent(this.vscroll.nativeElement, 'scroll').subscribe((e) => {
  (document.body.querySelector('#headerScroll') as HTMLElement).style.marginLeft = -this.vscroll.nativeElement.scrollLeft + 'px';
});

In my ponent those two function won't happen at the same time, so what should I do to make it only trigger one function and prevent from the other being executed?

I want to listen scroll event and handler them by two function for two scroll.

One need a throttleTime() to save resource and the other cannot use throttleTime() because it need trigger immediately and it doesn't cast too much.

Now I add two fromEvent to handle them but question is they will both be fired if scroll event is triggered.

fromEvent(this.vscroll.nativeElement, 'scroll').pipe(throttleTime(200)).subscribe((e) => {
  if (this.timer) {
    clearTimeout(this.timer);
    this.timer = null;
    this.timer = setTimeout(() => {
      this.updateShow();
    }, 201);
  } else {
    this.timer = setTimeout(() => {
      this.updateShow();
    }, 201);
  }
});
fromEvent(this.vscroll.nativeElement, 'scroll').subscribe((e) => {
  (document.body.querySelector('#headerScroll') as HTMLElement).style.marginLeft = -this.vscroll.nativeElement.scrollLeft + 'px';
});

In my ponent those two function won't happen at the same time, so what should I do to make it only trigger one function and prevent from the other being executed?

Share edited Dec 13, 2020 at 22:17 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 12, 2018 at 9:13 LoicLoic 1841 gold badge2 silver badges14 bronze badges 1
  • Put them in the same event handler, call 2 functions based on the condition whether there was a vertical scroll or a horizontal one. – Dennis Lukas Commented Nov 12, 2018 at 9:56
Add a ment  | 

1 Answer 1

Reset to default 5

Create your single function but store the previous X and Y scroll positions so you can see if they've changed between calls:

var scrollY = 0;
var scrollX = 0;
function onScroll(){
  var doc = document.documentElement;
  if(doc.scrollLeft !== scrollX) {
    scrollX = doc.scrollLeft;
    //Put behaviour for X scroll here
    $("p").text("X SCROLLED!");
  }
  if(doc.scrollTop !== scrollY) {
    scrollY = doc.scrollTop;
    //Put behaviour for Y scroll here
    $("p").text("Y SCROLLED!");
  }
}

window.addEventListener('scroll', onScroll);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width:1000px; height:1000px; background-color:rebeccapurple; color:#FFF">
  <p style="position: fixed;"></p>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745053068a4608502.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信