mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat: add use-touch hook
This commit is contained in:
parent
f329fd9f6f
commit
0dfce17f6c
@ -1,5 +1,5 @@
|
|||||||
import { Ref, onMounted } from 'vue';
|
import { Ref } from 'vue';
|
||||||
import { useHandler } from './use-handler';
|
import { useGlobalEvent } from './use-global-event';
|
||||||
|
|
||||||
export type UseClickOutsideOpitons = {
|
export type UseClickOutsideOpitons = {
|
||||||
event: string;
|
event: string;
|
||||||
@ -17,7 +17,5 @@ export function useClickOutside(options: UseClickOutsideOpitons) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
useGlobalEvent(document, event, onClick, false, flag);
|
||||||
useHandler(document, event, onClick, false, flag);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import {
|
|||||||
onDeactivated
|
onDeactivated
|
||||||
} from 'vue';
|
} from 'vue';
|
||||||
|
|
||||||
export function useHandler(
|
export function useGlobalEvent(
|
||||||
target: EventTarget,
|
target: EventTarget,
|
||||||
event: string,
|
event: string,
|
||||||
handler: EventListener,
|
handler: EventListener,
|
63
src/hooks/use-touch.ts
Normal file
63
src/hooks/use-touch.ts
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import { ref } from 'vue';
|
||||||
|
|
||||||
|
const MIN_DISTANCE = 10;
|
||||||
|
|
||||||
|
function getDirection(x: number, y: number) {
|
||||||
|
if (x > y && x > MIN_DISTANCE) {
|
||||||
|
return 'horizontal';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y > x && y > MIN_DISTANCE) {
|
||||||
|
return 'vertical';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTouch() {
|
||||||
|
const startX = ref(0);
|
||||||
|
const startY = ref(0);
|
||||||
|
const deltaX = ref(0);
|
||||||
|
const deltaY = ref(0);
|
||||||
|
const offsetX = ref(0);
|
||||||
|
const offsetY = ref(0);
|
||||||
|
const direction = ref('');
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
direction.value = '';
|
||||||
|
deltaX.value = 0;
|
||||||
|
deltaY.value = 0;
|
||||||
|
offsetX.value = 0;
|
||||||
|
offsetY.value = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function start(event: TouchEvent) {
|
||||||
|
reset();
|
||||||
|
startX.value = event.touches[0].clientX;
|
||||||
|
startY.value = event.touches[0].clientY;
|
||||||
|
}
|
||||||
|
|
||||||
|
function move(event: TouchEvent) {
|
||||||
|
const touch = event.touches[0];
|
||||||
|
deltaX.value = touch.clientX - this.startX;
|
||||||
|
deltaY.value = touch.clientY - this.startY;
|
||||||
|
offsetX.value = Math.abs(this.deltaX);
|
||||||
|
offsetY.value = Math.abs(this.deltaY);
|
||||||
|
|
||||||
|
if (!direction.value) {
|
||||||
|
direction.value = getDirection(offsetX.value, offsetY.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
move,
|
||||||
|
start,
|
||||||
|
startX,
|
||||||
|
startY,
|
||||||
|
deltaX,
|
||||||
|
deltaY,
|
||||||
|
offsetX,
|
||||||
|
offsetY,
|
||||||
|
direction
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user