【发布时间】:2020-01-31 01:54:07
【问题描述】:
我有一个列表,overflow-x 和 overflow-y 设置为 auto。另外,我已经设置了动量滚动,所以触摸滚动在移动设备上效果很好,使用webkit-overflow-scrolling: true。
然而,问题是我无法弄清楚如何在垂直滚动时禁用水平滚动。它会导致非常糟糕的用户体验,因为向左上角或右上角滑动会导致表格沿对角线滚动。当用户垂直滚动时,我绝对不希望在用户停止垂直滚动之前进行任何水平滚动。
我尝试了以下方法:
JS:
offsetX: number;
offsetY: number;
isScrollingHorizontally = false;
isScrollingVertically = false;
//Detect the scrolling events
ngOnInit() {
this.scrollListener = this.renderer.listen(
this.taskRows.nativeElement,
'scroll',
evt => {
this.didScroll();
}
);
fromEvent(this.taskRows.nativeElement, 'scroll')
.pipe(
debounceTime(100),
takeUntil(this.destroy$)
)
.subscribe(() => {
this.endScroll();
});
}
didScroll() {
if ((this.taskRows.nativeElement.scrollLeft != this.offsetX) && (!this.isScrollingHorizontally)){
console.log("Scrolling horizontally")
this.isScrollingHorizontally = true;
this.isScrollingVertically = false;
this.changeDetectorRef.markForCheck();
}else if ((this.taskRows.nativeElement.scrollTop != this.offsetY) && (!this.isScrollingVertically)) {
console.log("Scrolling Vertically")
this.isScrollingHorizontally = false;
this.isScrollingVertically = true;
this.changeDetectorRef.markForCheck();
}
}
endScroll() {
console.log("Ended scroll")
this.isScrollingVertically = false;
this.isScrollingHorizontally = false;
this.changeDetectorRef.markForCheck();
}
HTML:
<div
class="cu-dashboard-table__scroll"
[class.cu-dashboard-table__scroll_disable-x]="isScrollingVertically"
[class.cu-dashboard-table__scroll_disable-y]="isScrollingHorizontally"
>
CSS:
&__scroll {
display: flex;
width: 100%;
height: 100%;
overflow-y: auto;
overflow-x: auto;
will-change: transform;
-webkit-overflow-scrolling: touch;
&_disable-x {
overflow-x: hidden;
}
&_disable-y {
overflow-y: hidden;
}
}
但是每次我在滚动时将overflow-x 或overflow-y 设置为hidden,滚动都会出现故障并跳回顶部。我还注意到webkit-overflow-scrolling: true 是发生这种情况的原因,当我删除它时,行为似乎停止了,但我绝对需要它来在移动设备中进行动量滚动。
垂直滚动时如何禁用水平滚动?
【问题讨论】:
-
我不知道如何解决滚动问题。也许退后一步并尝试阻止两个轴之一滚动?题外话:表格不适用于非常小的屏幕。
-
您的代码中缺少一个大括号。如果将其添加为 sn-p,您将在控制台中看到一条错误消息。
-
只是一个愚蠢的想法。为什么不使用两个滑块或自定义滑块来滚动?
标签: javascript html css angular ionic-framework