-
Notifications
You must be signed in to change notification settings - Fork 163
feat: mouse down to scroll #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import raf from 'rc-util/lib/raf'; | ||
import * as React from 'react'; | ||
|
||
function smoothScrollOffset(offset: number) { | ||
return Math.floor(offset ** 0.5); | ||
} | ||
|
||
export function getPageXY( | ||
e: React.MouseEvent | React.TouchEvent | MouseEvent | TouchEvent, | ||
horizontal: boolean, | ||
) { | ||
const obj = 'touches' in e ? e.touches[0] : e; | ||
return obj[horizontal ? 'pageX' : 'pageY']; | ||
} | ||
|
||
export default function useScrollDrag( | ||
inVirtual: boolean, | ||
componentRef: React.RefObject<HTMLElement>, | ||
onScrollOffset: (offset: number) => void, | ||
) { | ||
React.useEffect(() => { | ||
const ele = componentRef.current; | ||
if (inVirtual && ele) { | ||
let mouseDownLock = false; | ||
let rafId: number; | ||
let offset: number; | ||
|
||
const stopScroll = () => { | ||
raf.cancel(rafId); | ||
}; | ||
|
||
const continueScroll = () => { | ||
stopScroll(); | ||
|
||
rafId = raf(() => { | ||
onScrollOffset(offset); | ||
continueScroll(); | ||
}); | ||
}; | ||
|
||
const onMouseDown = (e: MouseEvent) => { | ||
// Skip if nest List has handled this event | ||
const event = e as MouseEvent & { | ||
_virtualHandled?: boolean; | ||
}; | ||
if (!event._virtualHandled) { | ||
event._virtualHandled = true; | ||
mouseDownLock = true; | ||
} | ||
}; | ||
const onMouseUp = () => { | ||
mouseDownLock = false; | ||
stopScroll(); | ||
}; | ||
const onMouseMove = (e: MouseEvent) => { | ||
if (mouseDownLock) { | ||
const mouseY = getPageXY(e, false); | ||
const { top, bottom } = ele.getBoundingClientRect(); | ||
|
||
if (mouseY <= top) { | ||
const diff = top - mouseY; | ||
offset = -smoothScrollOffset(diff); | ||
continueScroll(); | ||
} else if (mouseY >= bottom) { | ||
const diff = mouseY - bottom; | ||
offset = smoothScrollOffset(diff); | ||
continueScroll(); | ||
} else { | ||
stopScroll(); | ||
} | ||
} | ||
}; | ||
|
||
ele.addEventListener('mousedown', onMouseDown); | ||
ele.ownerDocument.addEventListener('mouseup', onMouseUp); | ||
ele.ownerDocument.addEventListener('mousemove', onMouseMove); | ||
|
||
return () => { | ||
ele.removeEventListener('mousedown', onMouseDown); | ||
ele.ownerDocument.removeEventListener('mouseup', onMouseUp); | ||
ele.ownerDocument.removeEventListener('mousemove', onMouseMove); | ||
stopScroll(); | ||
}; | ||
} | ||
}, [inVirtual]); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
处理
smoothScrollOffset
函数中的负偏移值当
offset
为负数时,smoothScrollOffset
函数将返回NaN
,因为在 JavaScript 中,负数的 0.5 次幂结果为NaN
。这可能导致当offset
为负数时出现问题。建议在计算前对offset
取绝对值,并根据原始符号返回结果。建议修改如下:
📝 Committable suggestion