parent
dbb93fd76c
commit
40a785a538
1 changed files with 31 additions and 15 deletions
|
@ -46,41 +46,56 @@ const PullDownToRefresh: Component<{
|
||||||
let lts = -1;
|
let lts = -1;
|
||||||
let ds = 0;
|
let ds = 0;
|
||||||
let holding = false;
|
let holding = false;
|
||||||
const M = 1;
|
const K = 5;
|
||||||
const K = -10;
|
|
||||||
const D = -10;
|
|
||||||
const updatePullDown = (ts: number) => {
|
const updatePullDown = (ts: number) => {
|
||||||
released = false;
|
released = false;
|
||||||
try {
|
try {
|
||||||
const x = untrack(pullDown);
|
const x = untrack(pullDown);
|
||||||
const dt = lts !== -1 ? ts - lts : 1 / 60;
|
const dt = lts !== -1 ? ts - lts : 1 / 60;
|
||||||
const fs = (ds / Math.pow(dt, 2)) * M;
|
const vspring = holding ? 0 : K * x * dt;
|
||||||
const fh = (x + ds) * K + D * v;
|
v = ds / dt - vspring;
|
||||||
const f = fs + fh;
|
|
||||||
const a = f / M;
|
|
||||||
v += a * dt;
|
|
||||||
if (holding && v < 0) {
|
|
||||||
v = 0
|
|
||||||
}
|
|
||||||
setPullDown(x + v * dt);
|
setPullDown(x + v * dt);
|
||||||
|
|
||||||
if (Math.abs(x) > 1 || Math.abs(v) > 1) {
|
if (Math.abs(x) > 1 || Math.abs(v) > 1) {
|
||||||
requestAnimationFrame(updatePullDown);
|
requestAnimationFrame(updatePullDown);
|
||||||
} else {
|
} else {
|
||||||
v = 0;
|
v = 0;
|
||||||
lts = -1;
|
lts = -1;
|
||||||
}
|
}
|
||||||
if (!holding && untrack(pullDownDistance) >= 160 && !props.loading && props.onRefresh) {
|
|
||||||
setTimeout(props.onRefresh, 0)
|
if (
|
||||||
|
!holding &&
|
||||||
|
untrack(pullDownDistance) >= 160 &&
|
||||||
|
!props.loading &&
|
||||||
|
props.onRefresh
|
||||||
|
) {
|
||||||
|
setTimeout(props.onRefresh, 0);
|
||||||
|
// FIXME: this may trigger at multiple frames
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
ds = 0;
|
ds = 0;
|
||||||
released = true;
|
released = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let wheelTimeout: ReturnType<typeof setTimeout> | undefined;
|
||||||
|
|
||||||
|
const onWheelNotUpdated = () => {
|
||||||
|
wheelTimeout = undefined;
|
||||||
|
holding = false;
|
||||||
|
};
|
||||||
|
|
||||||
const handleLinkedWheel = (event: WheelEvent) => {
|
const handleLinkedWheel = (event: WheelEvent) => {
|
||||||
const scrollTop = (event.target as HTMLElement).scrollTop;
|
const scrollTop = (event.target as HTMLElement).scrollTop;
|
||||||
if (scrollTop >= 0 && scrollTop < 1) {
|
if (scrollTop >= 0 && scrollTop < 1) {
|
||||||
ds = -(event.deltaY / window.devicePixelRatio / 4);
|
event.preventDefault()
|
||||||
|
ds = -(event.deltaY / window.devicePixelRatio / 2);
|
||||||
|
holding = true;
|
||||||
|
if (wheelTimeout) {
|
||||||
|
clearTimeout(wheelTimeout);
|
||||||
|
}
|
||||||
|
wheelTimeout = setTimeout(onWheelNotUpdated, 200);
|
||||||
if (released) {
|
if (released) {
|
||||||
released = false;
|
released = false;
|
||||||
requestAnimationFrame(updatePullDown);
|
requestAnimationFrame(updatePullDown);
|
||||||
|
@ -107,6 +122,7 @@ const PullDownToRefresh: Component<{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const item = event.targetTouches.item(0)!;
|
const item = event.targetTouches.item(0)!;
|
||||||
|
event.preventDefault();
|
||||||
if (lastTouchId && item.identifier !== lastTouchId) {
|
if (lastTouchId && item.identifier !== lastTouchId) {
|
||||||
lastTouchId = undefined;
|
lastTouchId = undefined;
|
||||||
lastTouchScreenY = 0;
|
lastTouchScreenY = 0;
|
||||||
|
@ -114,7 +130,7 @@ const PullDownToRefresh: Component<{
|
||||||
}
|
}
|
||||||
holding = true;
|
holding = true;
|
||||||
if (lastTouchScreenY !== 0) {
|
if (lastTouchScreenY !== 0) {
|
||||||
ds = item.screenY - lastTouchScreenY;
|
ds = (item.screenY - lastTouchScreenY) / window.devicePixelRatio;
|
||||||
}
|
}
|
||||||
lastTouchScreenY = item.screenY;
|
lastTouchScreenY = item.screenY;
|
||||||
if (released) {
|
if (released) {
|
||||||
|
|
Loading…
Reference in a new issue