Skip to content

Commit 6dcbac2

Browse files
fix resize-scroll when gridster is not at top of page (#1727)
* fix resize-scroll when gridster is not at top of page
1 parent 498c1df commit 6dcbac2

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<link rel="stylesheet" href="../../../demo/demo.css"/>
8+
<!-- <script src="https://cdn.jsdelivr.net/npm/gridstack@3.3.0/dist/gridstack-h5.js"></script> -->
9+
<script src="../../../dist/gridstack-h5.js"></script>
10+
<style>
11+
.topHeader {
12+
background: rgb(127, 87, 180);
13+
height: 200px;
14+
}
15+
.willScroll {
16+
height: 600px;
17+
overflow-y: auto;
18+
flex-grow: 1;
19+
}
20+
.row {
21+
display:flex;
22+
width: 100%;
23+
}
24+
.showDistance {
25+
border-top-width: 130px;
26+
border-bottom-width: 130px;
27+
border-color: gray;
28+
border-style: solid;
29+
height: 600px;
30+
width: 50px;
31+
box-sizing: border-box;
32+
}
33+
</style>
34+
35+
</head>
36+
<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>
43+
</div>
44+
45+
<script type="text/javascript">
46+
let grid = GridStack.init({cellHeight: 130});
47+
let items = [
48+
{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."},
49+
{x: 3, y: 0, w: 2, h: 2, content: "this is here to have next item positioned way down"},
50+
{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"},
52+
];
53+
grid.load(items);
54+
</script>
55+
</body>
56+
</html>

src/utils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,17 @@ export class Utils {
351351
static updateScrollResize(event: MouseEvent, el: HTMLElement, distance: number): void {
352352
const scrollEl = this.getScrollParent(el);
353353
const height = scrollEl.clientHeight;
354-
355-
const top = event.clientY < distance;
356-
const bottom = event.clientY > height - distance;
354+
const offsetTop = scrollEl.getBoundingClientRect().top;
355+
const pointerPosY = event.clientY - offsetTop;
356+
const top = pointerPosY < distance;
357+
const bottom = pointerPosY > height - distance;
357358

358359
if (top) {
359360
// This also can be done with a timeout to keep scrolling while the mouse is
360361
// in the scrolling zone. (will have smoother behavior)
361-
scrollEl.scrollBy({ behavior: 'smooth', top: event.clientY - distance});
362+
scrollEl.scrollBy({ behavior: 'smooth', top: pointerPosY - distance});
362363
} else if (bottom) {
363-
scrollEl.scrollBy({ behavior: 'smooth', top: distance - (height - event.clientY)});
364+
scrollEl.scrollBy({ behavior: 'smooth', top: distance - (height - pointerPosY)});
364365
}
365366
}
366367
}

0 commit comments

Comments
 (0)