Skip to content

useSwipe

Category
Export Size
996 B
Last Changed
15 minutes ago

基于 TouchEvents 的响应式滑动检测。

Demo

向右滑动

方向:none
水平长度:0 | 垂直长度:0

用法

vue
<script setup>
const el = ref(null)
const { isSwiping, direction } = useSwipe(el)
</script>

<template>
  <div ref="el">
    在这里滑动
  </div>
</template>

类型声明

显示类型声明
typescript
export type UseSwipeDirection = "up" | "down" | "left" | "right" | "none"
export interface UseSwipeOptions extends ConfigurableWindow {
  /**
   * 将事件注册为被动事件
   *
   * @default true
   */
  passive?: boolean
  /**
   * @default 50
   */
  threshold?: number
  /**
   * 滑动开始时的回调函数
   */
  onSwipeStart?: (e: TouchEvent) => void
  /**
   * 滑动过程中的回调函数
   */
  onSwipe?: (e: TouchEvent) => void
  /**
   * 滑动结束时的回调函数
   */
  onSwipeEnd?: (e: TouchEvent, direction: UseSwipeDirection) => void
}
export interface UseSwipeReturn {
  isPassiveEventSupported: boolean
  isSwiping: Ref<boolean>
  direction: ComputedRef<UseSwipeDirection>
  coordsStart: Readonly<Position>
  coordsEnd: Readonly<Position>
  lengthX: ComputedRef<number>
  lengthY: ComputedRef<number>
  stop: () => void
}
/**
 * 响应式滑动检测。
 *
 * @see https://vueuse.org/useSwipe
 * @param target
 * @param options
 */
export declare function useSwipe(
  target: MaybeRefOrGetter<EventTarget | null | undefined>,
  options?: UseSwipeOptions,
): UseSwipeReturn

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Amirreza Zarkesh
lstoeferle
Anthony Fu
Alexey Istomin
Jelf
云游君
Shinigami
Alex Kozack

变更日志

v10.7.0 on 12/5/2023
fccf2 - feat: upgrade deps (#3614)
v10.1.0 on 4/22/2023
af3ca - fix: ignore multiple touches (#2996)
995aa - fix: ignore multiple touches (#2994)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
v10.0.0-beta.0 on 3/14/2023
f285c - fix!: use literal for swipe direction instead of enum
08c21 - feat(useSwipe, usePointerSwipe, useTransition): improve tree-shaking (#2863)

Released under the MIT License.