Skip to content

Commit 81bcbd3

Browse files
fix scroll problem when scrollbar on html element (#1751)
fix scroll problem when scrollbar on html element
1 parent 381f2fb commit 81bcbd3

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

spec/e2e/html/1727_resize_scroll_top.html

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
<link rel="stylesheet" href="../../../demo/demo.css"/>
8-
<!-- <script src="https://cdn.jsdelivr.net/npm/gridstack@3.3.0/dist/gridstack-h5.js"></script> -->
98
<script src="../../../dist/gridstack-h5.js"></script>
109
<style>
1110
.topHeader {
@@ -30,16 +29,37 @@
3029
width: 50px;
3130
box-sizing: border-box;
3231
}
32+
.outer {
33+
display: flex;
34+
}
35+
.main {
36+
flex-grow: 1;
37+
}
38+
.bodyScroll {
39+
height: 2000px;
40+
width: 100px;
41+
flex: 0 0 auto;
42+
border: 3px solid blue;
43+
}
3344
</style>
3445

3546
</head>
3647
<body>
37-
<div class="topHeader">This is the header which brings an offset of 200 px</div>
38-
<div class="row">
39-
<div class="showDistance"></div>
40-
<div class="willScroll">
41-
<div class="grid-stack"></div>
42-
</div>
48+
<div class="outer">
49+
<div class="main">
50+
<div class="topHeader">
51+
This page allows to test scrolling behavior when scroll element is not whole page (html element),
52+
and when the scrolling element is not at the top of the page. Also effects of multiple
53+
scrollbars can be tested, as the html element also can be scrolled.<br>
54+
This is the header which brings an offset of 200 px</div>
55+
<div class="row">
56+
<div class="showDistance"></div>
57+
<div class="willScroll">
58+
<div class="grid-stack"></div>
59+
</div>
60+
</div>
61+
</div>
62+
<div class="bodyScroll">This causes the scrollbar on html element</div>
4363
</div>
4464

4565
<script type="text/javascript">
@@ -48,7 +68,7 @@
4868
{x: 0, y: 0, w: 2, h: 2, content: "resize-me - check scrolling starts only when in height of gray area, and you can scroll back up as well."},
4969
{x: 3, y: 0, w: 2, h: 2, content: "this is here to have next item positioned way down"},
5070
{x: 3, y: 2, w: 3, h: 2, content: "resize me - scroll will start immediately, but should not be extremely fast"},
51-
{x: 6, y: 0, w: 2, h: 8, content: "this is here to have a scroll bar from the beginning"},
71+
{x: 6, y: 0, w: 2, h: 8, content: "this is here to have a scroll bar on .main div from the beginning"},
5272
];
5373
grid.load(items);
5474
</script>

src/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,19 @@ export class Utils {
351351
static updateScrollResize(event: MouseEvent, el: HTMLElement, distance: number): void {
352352
const scrollEl = this.getScrollElement(el);
353353
const height = scrollEl.clientHeight;
354-
const offsetTop = scrollEl.getBoundingClientRect().top;
354+
// #1745 #1727 - check if mouse position is leaving scroll element.
355+
// event.clientY is relative to origin of viewport, must compare this against position of scrollEl,
356+
// accessible via clientHeight and getBoundingClientRect().top.
357+
// Special situation if scroll element is 'html': here browser spec states that
358+
// clientHeight is height of viewport, but getBoundingClientRect() is rectangle of html element;
359+
// this discrepancy arises because in reality scrollbar is attached to viewport, not html element itself.
360+
let offsetTop : number;
361+
if (scrollEl === this.getScrollElement()) {
362+
offsetTop = 0;
363+
} else {
364+
offsetTop = scrollEl.getBoundingClientRect().top;
365+
}
366+
355367
const pointerPosY = event.clientY - offsetTop;
356368
const top = pointerPosY < distance;
357369
const bottom = pointerPosY > height - distance;

0 commit comments

Comments
 (0)