mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
types: fix event listener typing
This commit is contained in:
parent
0a701e549c
commit
1237ee6c2c
@ -8,7 +8,7 @@ const CLASSNAME = 'van-overflow-hidden';
|
|||||||
export function useLockScroll(element: HTMLElement) {
|
export function useLockScroll(element: HTMLElement) {
|
||||||
const { start, move, deltaY, direction } = useTouch();
|
const { start, move, deltaY, direction } = useTouch();
|
||||||
|
|
||||||
function onTouchMove(event: TouchEvent) {
|
const onTouchMove = ((event: TouchEvent) => {
|
||||||
move(event);
|
move(event);
|
||||||
|
|
||||||
if (direction.value !== 'vertical') {
|
if (direction.value !== 'vertical') {
|
||||||
@ -29,9 +29,9 @@ export function useLockScroll(element: HTMLElement) {
|
|||||||
if (prevent) {
|
if (prevent) {
|
||||||
preventDefault(event, true);
|
preventDefault(event, true);
|
||||||
}
|
}
|
||||||
}
|
}) as EventListener;
|
||||||
|
|
||||||
function lock() {
|
const lock = () => {
|
||||||
if (!count) {
|
if (!count) {
|
||||||
document.body.classList.add(CLASSNAME);
|
document.body.classList.add(CLASSNAME);
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ export function useLockScroll(element: HTMLElement) {
|
|||||||
count++;
|
count++;
|
||||||
on(document, 'touchstart', start);
|
on(document, 'touchstart', start);
|
||||||
on(document, 'touchmove', onTouchMove);
|
on(document, 'touchmove', onTouchMove);
|
||||||
}
|
};
|
||||||
|
|
||||||
lock();
|
lock();
|
||||||
|
|
||||||
|
@ -23,21 +23,21 @@ export function useTouch() {
|
|||||||
const offsetY = ref(0);
|
const offsetY = ref(0);
|
||||||
const direction = ref('');
|
const direction = ref('');
|
||||||
|
|
||||||
function reset() {
|
const reset = () => {
|
||||||
direction.value = '';
|
|
||||||
deltaX.value = 0;
|
deltaX.value = 0;
|
||||||
deltaY.value = 0;
|
deltaY.value = 0;
|
||||||
offsetX.value = 0;
|
offsetX.value = 0;
|
||||||
offsetY.value = 0;
|
offsetY.value = 0;
|
||||||
}
|
direction.value = '';
|
||||||
|
};
|
||||||
|
|
||||||
function start(event: TouchEvent) {
|
const start = ((event: TouchEvent) => {
|
||||||
reset();
|
reset();
|
||||||
startX.value = event.touches[0].clientX;
|
startX.value = event.touches[0].clientX;
|
||||||
startY.value = event.touches[0].clientY;
|
startY.value = event.touches[0].clientY;
|
||||||
}
|
}) as EventListener;
|
||||||
|
|
||||||
function move(event: TouchEvent) {
|
const move = ((event: TouchEvent) => {
|
||||||
const touch = event.touches[0];
|
const touch = event.touches[0];
|
||||||
deltaX.value = touch.clientX - startX.value;
|
deltaX.value = touch.clientX - startX.value;
|
||||||
deltaY.value = touch.clientY - startY.value;
|
deltaY.value = touch.clientY - startY.value;
|
||||||
@ -47,7 +47,7 @@ export function useTouch() {
|
|||||||
if (!direction.value) {
|
if (!direction.value) {
|
||||||
direction.value = getDirection(offsetX.value, offsetY.value);
|
direction.value = getDirection(offsetX.value, offsetY.value);
|
||||||
}
|
}
|
||||||
}
|
}) as EventListener;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
move,
|
move,
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import { inBrowser } from '..';
|
import { inBrowser } from '..';
|
||||||
|
|
||||||
type EventHandler = (event: Event) => void;
|
|
||||||
|
|
||||||
// eslint-disable-next-line import/no-mutable-exports
|
// eslint-disable-next-line import/no-mutable-exports
|
||||||
export let supportsPassive = false;
|
export let supportsPassive = false;
|
||||||
|
|
||||||
@ -23,7 +21,7 @@ if (inBrowser) {
|
|||||||
export function on(
|
export function on(
|
||||||
target: EventTarget,
|
target: EventTarget,
|
||||||
event: string,
|
event: string,
|
||||||
handler: EventHandler,
|
handler: EventListenerOrEventListenerObject,
|
||||||
passive = false
|
passive = false
|
||||||
) {
|
) {
|
||||||
if (inBrowser) {
|
if (inBrowser) {
|
||||||
@ -35,7 +33,11 @@ export function on(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function off(target: EventTarget, event: string, handler: EventHandler) {
|
export function off(
|
||||||
|
target: EventTarget,
|
||||||
|
event: string,
|
||||||
|
handler: EventListenerOrEventListenerObject
|
||||||
|
) {
|
||||||
if (inBrowser) {
|
if (inBrowser) {
|
||||||
target.removeEventListener(event, handler);
|
target.removeEventListener(event, handler);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user