跳到主要內容

useParallax

類別
導出大小
1.94 kB
上次變更
4 個月前

輕鬆建立視差效果。它使用 useDeviceOrientation,若不支援裝置方向則回退至 useMouse

示範

roll: .nan
tilt: .nan
source: mouse
圖片版權屬於 Jarom Vogel

用法

vue
<script setup>
import { useParallax } from '@vueuse/core'

const container = ref(null)
const { tilt, roll, source } = useParallax(container)
</script>

<template>
  <div ref="container" />
</template>

類型宣告

typescript
export interface UseParallaxOptions extends ConfigurableWindow {
  deviceOrientationTiltAdjust?: (i: number) => number
  deviceOrientationRollAdjust?: (i: number) => number
  mouseTiltAdjust?: (i: number) => number
  mouseRollAdjust?: (i: number) => number
}
export interface UseParallaxReturn {
  /**
   * Roll value. Scaled to `-0.5 ~ 0.5`
   */
  roll: ComputedRef<number>
  /**
   * Tilt value. Scaled to `-0.5 ~ 0.5`
   */
  tilt: ComputedRef<number>
  /**
   * Sensor source, can be `mouse` or `deviceOrientation`
   */
  source: ComputedRef<"deviceOrientation" | "mouse">
}
/**
 * Create parallax effect easily. It uses `useDeviceOrientation` and fallback to `useMouse`
 * if orientation is not supported.
 *
 * @param target
 * @param options
 */
export declare function useParallax(
  target: MaybeElementRef,
  options?: UseParallaxOptions,
): UseParallaxReturn

原始碼

原始碼示範文件

貢獻者

Anthony Fu
Antério Vieira
Anthony Fu
IlyaL
Robin
huiliangShen
Jelf
wheat

更新日誌

v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化 bundles 並清理程式碼 (#4349)
v10.8.0 於 2024/2/20
3fd94 - feat: 可以使用不同的螢幕方向 (#3675)

在 MIT 許可證下發布。