Skip to content

Commit 4f77677

Browse files
committedApr 23, 2022
- fix bug: incorrect position when scrolling if scroll element is
outside the absolute / relative positioned element.
1 parent 5433528 commit 4f77677

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed
 

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
- fix bug: hour / minute don't work
1111
- support enter / escape to close
1212
- let dialog be mutual exclusive
13+
- fix bug: incorrect position when scrolling if scroll element is outside the absolute / relative positioned element.
14+
1315

1416
## v0.0.2
1517

‎dist/index.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
return this.root.classList.contains('active');
179179
},
180180
toggle: function(v){
181-
var c, h, n, hb, cb, ref$, x, y, nscroll, nstack, s, stackb, scrollb;
181+
var c, h, n, hb, cb, ref$, x, y, nscroll, nstack, countScroll, s, stackb, scrollb, scroll;
182182
if (arguments.length === 0) {
183183
v = !this.root.classList.contains('active');
184184
}
@@ -200,6 +200,7 @@
200200
cb = c.getBoundingClientRect();
201201
ref$ = [0, 0], x = ref$[0], y = ref$[1];
202202
ref$ = [null, null], nscroll = ref$[0], nstack = ref$[1];
203+
countScroll = true;
203204
while (n && n.getAttribute) {
204205
s = getComputedStyle(n);
205206
if (n.nodeName === 'BODY' || ['overflow', 'overflow-y', 'overflow-x'].filter(fn$).length) {
@@ -210,6 +211,9 @@
210211
if (n.nodeName === 'BODY' || s.position !== 'static') {
211212
if (!nstack) {
212213
nstack = n;
214+
if (!nscroll) {
215+
countScroll = false;
216+
}
213217
}
214218
}
215219
if (nscroll && nstack) {
@@ -219,15 +223,24 @@
219223
}
220224
stackb = nstack.getBoundingClientRect();
221225
scrollb = nscroll.getBoundingClientRect();
226+
scroll = countScroll
227+
? {
228+
left: nscroll.scrollLeft,
229+
top: nscroll.scrollTop
230+
}
231+
: {
232+
left: 0,
233+
top: 0
234+
};
222235
if (hb.y + hb.height + cb.height > scrollb.y + scrollb.height) {
223-
y = hb.y - stackb.y - cb.height + nscroll.scrollTop - 2;
236+
y = hb.y - stackb.y - cb.height + scroll.top - 2;
224237
} else {
225-
y = hb.y - stackb.y + hb.height + nscroll.scrollTop + 2;
238+
y = hb.y - stackb.y + hb.height + scroll.top + 2;
226239
}
227240
if (hb.x + cb.width > scrollb.x + scrollb.width) {
228-
x = hb.x - stackb.x + hb.width - cb.width + nscroll.scrollLeft;
241+
x = hb.x - stackb.x + hb.width - cb.width + scroll.left;
229242
} else {
230-
x = hb.x - stackb.x + nscroll.scrollLeft;
243+
x = hb.x - stackb.x + scroll.left;
231244
}
232245
c.style.transform = "translate(" + x + "px, " + y + "px)";
233246
return ref$ = c.style, ref$.top = 0, ref$.left = 0, ref$;

‎dist/index.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/index.ls

+13-5
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,33 @@ lddatetimepicker.prototype = Object.create(Object.prototype) <<< do
145145
cb = c.getBoundingClientRect!
146146
[x,y] = [0,0]
147147
[nscroll, nstack] = [null, null]
148+
# should we adjust picker position based on scroll element?
149+
count-scroll = true
148150

149151
while n and n.getAttribute
150152
s = getComputedStyle(n)
151153
if n.nodeName == \BODY or <[overflow overflow-y overflow-x]>.filter(-> s[it] != \visible).length =>
152154
if !nscroll => nscroll = n
155+
# TODO (should we consider opacity, transform and filter? )
153156
if n.nodeName == \BODY or s.position != \static =>
154-
if !nstack => nstack = n
157+
if !nstack =>
158+
nstack = n
159+
# no scroll element within stacking context.
160+
# thus, scroll left/top should not be used when calculating position
161+
if !nscroll => count-scroll = false
155162
if nscroll and nstack => break
156163
n = n.parentNode
157164
stackb = nstack.getBoundingClientRect!
158165
scrollb = nscroll.getBoundingClientRect!
166+
scroll = if count-scroll => {left: nscroll.scrollLeft, top: nscroll.scrollTop} else {left: 0, top: 0}
159167
if hb.y + hb.height + cb.height > scrollb.y + scrollb.height =>
160-
y = hb.y - stackb.y - cb.height + nscroll.scrollTop - 2
168+
y = hb.y - stackb.y - cb.height + scroll.top - 2
161169
else
162-
y = hb.y - stackb.y + hb.height + nscroll.scrollTop + 2
170+
y = hb.y - stackb.y + hb.height + scroll.top + 2
163171
if hb.x + cb.width > scrollb.x + scrollb.width =>
164-
x = hb.x - stackb.x + hb.width - cb.width + nscroll.scrollLeft
172+
x = hb.x - stackb.x + hb.width - cb.width + scroll.left
165173
else
166-
x = hb.x - stackb.x + nscroll.scrollLeft
174+
x = hb.x - stackb.x + scroll.left
167175

168176
c.style.transform = "translate(#{x}px, #{y}px)"
169177
c.style <<< top: 0, left: 0

0 commit comments

Comments
 (0)
Please sign in to comment.