如何只触发一次鼠标滚轮滚动
How to fire only once Mousewheel scroll
我正在尝试使用鼠标滚轮滚动来执行一些操作。我想要无论你滚动多快或多少次滚轮,它都算作 "one"。现在,如果您快速滚动,它会计算更多次。
保存滚动方向并在类似
的条件下使用它
scrollDirection:'up'|'down';
@HostListener('wheel', ['$event']) onMouseScroll(e) {
if (e.deltaY < 0 && this.scrollDirection !='up') {
console.log('scrolling up');
this.scrollDirection='up';
} else if (e.deltaY > 0 && this.scrollDirection !='down') {
console.log('scrolling down');
this.scrollDirection='down';
}
}
有一个计时器来限制滚动一定时间并保持滚动方向以检测滚动方向的变化。
import { Directive, HostListener } from '@angular/core';
import { timer } from 'rxjs';
@Directive({
selector: '[appMouseScroll]'
})
export class MouseScrollDirective {
Ttimer;
isMouseWheelInRogress;
scrollUp;
@HostListener('wheel', ['$event']) onMouseScroll(e) {
if (!this.isMouseWheelInRogress || (!this.scrollUp && e.deltaY < 0 || this.scrollUp && e.deltaY > 0)) {
if (this.Ttimer) {
this.Ttimer.unsubscribe();
}
this.Ttimer = timer(500).subscribe(() => {
this.isMouseWheelInRogress = false;
});
if (e.deltaY < 0) {
this.scrollUp = true;
console.log('scrolling up');
} else if (e.deltaY > 0) {
this.scrollUp = false;
console.log('scrolling down');
}
}
this.isMouseWheelInRogress = true;
}
}
你需要有一个超时功能,以避免所有的滚动事件,只触发最后一个事件。
我正在尝试使用鼠标滚轮滚动来执行一些操作。我想要无论你滚动多快或多少次滚轮,它都算作 "one"。现在,如果您快速滚动,它会计算更多次。
保存滚动方向并在类似
的条件下使用它scrollDirection:'up'|'down';
@HostListener('wheel', ['$event']) onMouseScroll(e) {
if (e.deltaY < 0 && this.scrollDirection !='up') {
console.log('scrolling up');
this.scrollDirection='up';
} else if (e.deltaY > 0 && this.scrollDirection !='down') {
console.log('scrolling down');
this.scrollDirection='down';
}
}
有一个计时器来限制滚动一定时间并保持滚动方向以检测滚动方向的变化。
import { Directive, HostListener } from '@angular/core';
import { timer } from 'rxjs';
@Directive({
selector: '[appMouseScroll]'
})
export class MouseScrollDirective {
Ttimer;
isMouseWheelInRogress;
scrollUp;
@HostListener('wheel', ['$event']) onMouseScroll(e) {
if (!this.isMouseWheelInRogress || (!this.scrollUp && e.deltaY < 0 || this.scrollUp && e.deltaY > 0)) {
if (this.Ttimer) {
this.Ttimer.unsubscribe();
}
this.Ttimer = timer(500).subscribe(() => {
this.isMouseWheelInRogress = false;
});
if (e.deltaY < 0) {
this.scrollUp = true;
console.log('scrolling up');
} else if (e.deltaY > 0) {
this.scrollUp = false;
console.log('scrolling down');
}
}
this.isMouseWheelInRogress = true;
}
}
你需要有一个超时功能,以避免所有的滚动事件,只触发最后一个事件。