跳到内容

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 支持,优化捆绑包并清理 (#4349)
v10.8.0 于 2024/2/20
3fd94 - feat: 可以使用不同的屏幕方向 (#3675)

在 MIT 许可证下发布。