垂直滚动时不允许水平滚动(反之亦然) [英] Don't allow horizontal scroll when scrolling vertically (and vice versa)
问题描述
我有一个列表,其中overflow-x
和overflow-y
设置为auto
.此外,我已经设置了动量滚动,因此使用webkit-overflow-scrolling: true
在移动设备中触摸滚动效果很好.
I have a list, with the overflow-x
and overflow-y
set to auto
. In addition, I've set up momentum scroll, so the touch scrolling works nice in mobile, using webkit-overflow-scrolling: true
.
但是,问题是我无法弄清楚在垂直滚动时如何禁用水平滚动.这会导致非常糟糕的用户体验,因为向左上方或右上方滑动会导致表格沿对角线滚动.当用户垂直滚动时,我绝对不希望任何水平滚动,直到用户停止垂直滚动为止.
The issue, however, is that I cannot figure out how to disable the horizontal scroll when scrolling vertically. It leads to really bad user experience, as the swiping towards the top left or top right will cause the table to scroll diagonally. When the user is scrolling vertically, I absolutely do NOT want any scrolling horizontally until the user has stopped scrolling vertically.
我尝试了以下操作:
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
是发生这种情况的原因,当我将其删除时,该行为似乎停止了,但是我绝对需要使用它来在移动设备中进行动量滚动.
But the everytime I set overflow-x
or overflow-y
to hidden
when its been scrolled, scrolling will glitch and jump back to the top. I've also noticed that webkit-overflow-scrolling: true
is the reason why this occurs, when I remove it, the behavior seems to stop, but I absolutely need this for momentum scrolling in mobile devices.
如何在垂直滚动时禁用水平滚动?
How do I disable horizontal scroll when scrolling vertically?
推荐答案
尝试一下
HTML
<div
class="cu-dashboard-table__scroll-vertical"
>
<div
class="cu-dashboard-table__scroll-horizontal"
>
<!-- Scroll content here -->
</div>
</div>
CSS
&__scroll {
&-horizontal {
overflow-x: auto;
width: 100%;
-webkit-overflow-scrolling: touch;
}
&-vertical {
overflow-y: auto;
height: 100%;
-webkit-overflow-scrolling: touch;
}
}
您为什么不使用两个div而不是使用一个div滚动? X有一个,Y有一个
Instead of using one div for scrolling, why dont you use two? Have one for X and one for Y
这篇关于垂直滚动时不允许水平滚动(反之亦然)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!