Angular 中的 scrollIntoView 不会触发鼠标滚轮或滚动事件
scrollIntoView does not trigger mousewheel nor scroll event in Angular
我使用 Angular 并注册了 mousewheel
和 scroll
事件。当我使用 scrollIntoView
时,不会触发此事件。还有其他我想念的活动吗?
@HostListener('mousewheel', ['$event'])
mousewheel(event: MouseEvent) {
console.log("mousewheel");
}
@HostListener('scroll', ['$event'])
scroll(event: MouseEvent) {
console.log("scroll");
}
我使用的函数是这样的:
element.scrollIntoView({behavior: 'smooth'});
有人知道我在这里想念什么吗?
您始终可以使用 addEventListener 添加滚动功能。另外,不要忘记在 ngOnDestroy 中将其删除这是一个示例:
export class WindowScrollDirective implements OnInit, OnDestroy{
ngOnInit() {
window.addEventListener('scroll', this.handleScroll, true);
}
ngOnDestroy() {
window.removeEventListener('scroll', this.handleScroll, true);
}
handleScroll = (event): void => {
//handle your scroll here
};
}
注意:您可以用要用于滚动的元素替换 window 对象。
我使用 Angular 并注册了 mousewheel
和 scroll
事件。当我使用 scrollIntoView
时,不会触发此事件。还有其他我想念的活动吗?
@HostListener('mousewheel', ['$event'])
mousewheel(event: MouseEvent) {
console.log("mousewheel");
}
@HostListener('scroll', ['$event'])
scroll(event: MouseEvent) {
console.log("scroll");
}
我使用的函数是这样的:
element.scrollIntoView({behavior: 'smooth'});
有人知道我在这里想念什么吗?
您始终可以使用 addEventListener 添加滚动功能。另外,不要忘记在 ngOnDestroy 中将其删除这是一个示例:
export class WindowScrollDirective implements OnInit, OnDestroy{
ngOnInit() {
window.addEventListener('scroll', this.handleScroll, true);
}
ngOnDestroy() {
window.removeEventListener('scroll', this.handleScroll, true);
}
handleScroll = (event): void => {
//handle your scroll here
};
}
注意:您可以用要用于滚动的元素替换 window 对象。