@@ -13,9 +13,9 @@ export type dragOptions = {
13
13
move : ( a : dragParams ) => void ;
14
14
end : ( a : dragParams ) => void ;
15
15
cancel ?: ( ) => void ;
16
- mouseStableDistance : number ;
16
+ mouseStableDistance ? : number ;
17
17
touchStableDistance ?: number ;
18
- touchHoldTime ?: number ;
18
+ touchStableTime ?: number ;
19
19
} ;
20
20
export type dragParams = {
21
21
clientX : number ;
@@ -57,7 +57,7 @@ function getParamFromTouchEvent(event: TouchEvent, startX: number, startY: numbe
57
57
}
58
58
export function mouseDragHandle ( originEvent : MouseEvent | TouchEvent , options ?: dragOptions ) {
59
59
const event = originEvent as MouseEvent
60
- const { beforeStart, start, move, end, cancel, mouseStableDistance } = options || { }
60
+ const { beforeStart, start, move, end, cancel, mouseStableDistance = 20 } = options || { }
61
61
// 非左键不处理
62
62
if ( event . button !== 0 ) {
63
63
cancel && cancel ( )
@@ -105,7 +105,7 @@ export function mouseDragHandle(originEvent: MouseEvent | TouchEvent, options?:
105
105
106
106
export function touchDragHandle ( originEvent : MouseEvent | TouchEvent , options ?: dragOptions ) {
107
107
const event = originEvent as TouchEvent
108
- const { beforeStart, start, move, end, cancel, touchHoldTime = 300 , touchStableDistance = 50 } = options || { }
108
+ const { beforeStart, start, move, end, cancel, touchStableTime = 240 , touchStableDistance = 20 } = options || { }
109
109
const touches = event . touches || [ ]
110
110
const targetNode = event . target
111
111
if ( touches . length !== 1 || ! targetNode ) {
@@ -124,16 +124,15 @@ export function touchDragHandle(originEvent: MouseEvent | TouchEvent, options?:
124
124
start && start ( startX , startY )
125
125
const param = getParamFromTouchEvent ( event , startX , startY )
126
126
move && move ( param )
127
- } , touchHoldTime )
127
+ } , touchStableTime )
128
128
function touchMove ( originEvent : Event ) {
129
129
const event = originEvent as TouchEvent
130
130
const param = getParamFromTouchEvent ( event , startX , startY )
131
- if ( ! hasTriggerStartEvent && Math . sqrt ( param . xOffset * param . xOffset + param . yOffset * param . yOffset ) >
132
- touchStableDistance ) {
133
- touchCancel ( )
134
- return
135
- }
136
131
if ( ! hasTriggerStartEvent ) {
132
+ const moveDistance = Math . sqrt ( param . xOffset * param . xOffset + param . yOffset * param . yOffset )
133
+ if ( moveDistance > touchStableDistance ) {
134
+ touchCancel ( )
135
+ }
137
136
return
138
137
}
139
138
event . stopPropagation && event . stopPropagation ( )
@@ -167,8 +166,9 @@ export function touchDragHandle(originEvent: MouseEvent | TouchEvent, options?:
167
166
targetNode . addEventListener ( 'touchcancel' , touchCancel , listenerConfig )
168
167
}
169
168
169
+ export const supportTouch = 'ontouchend' in document
170
170
export default function ( event : MouseEvent | TouchEvent , options ?: dragOptions ) {
171
- if ( 'ontouchend' in document ) {
171
+ if ( supportTouch ) {
172
172
touchDragHandle ( event , options )
173
173
} else {
174
174
mouseDragHandle ( event , options )
0 commit comments