Skip to content

useSwipe

Category
Export Size
1.04 kB
Last Changed
3 hours ago

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

Demo

向右滑动

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

用法

vue
<script setup>
import { useTemplateRef } from 'vue'

const el = useTemplateRef('el')
const { isSwiping, direction } = useSwipe(el)
</script>

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

Type Declarations

Show Type Declarations
typescript
export type UseSwipeDirection = "up" | "down" | "left" | "right" | "none"
export interface UseSwipeOptions extends ConfigurableWindow {
  /**
   * Register events as passive
   *
   * @default true
   */
  passive?: boolean
  /**
   * @default 50
   */
  threshold?: number
  /**
   * Callback on swipe start
   */
  onSwipeStart?: (e: TouchEvent) => void
  /**
   * Callback on swipe moves
   */
  onSwipe?: (e: TouchEvent) => void
  /**
   * Callback on swipe ends
   */
  onSwipeEnd?: (e: TouchEvent, direction: UseSwipeDirection) => void
}
export interface UseSwipeReturn {
  /**
   * @deprecated No longer need this Vue 3's browser targets all supporting passive event listeners.
   *
   * This flag will always return `true` and be removed in the next major version.
   */
  isPassiveEventSupported: boolean
  isSwiping: ShallowRef<boolean>
  direction: ComputedRef<UseSwipeDirection>
  coordsStart: Readonly<Position>
  coordsEnd: Readonly<Position>
  lengthX: ComputedRef<number>
  lengthY: ComputedRef<number>
  stop: () => void
}
/**
 * Reactive swipe detection.
 *
 * @see https://vueuse.org/useSwipe
 * @param target
 * @param options
 */
export declare function useSwipe(
  target: MaybeRefOrGetter<EventTarget | null | undefined>,
  options?: UseSwipeOptions,
): UseSwipeReturn

Source

SourceDemoDocs

Contributors

jorshen

Changelog

No recent changes

Released under the MIT License.