跳到主要內容

useTransition

類別
導出大小
920 B
上次變更
5 天前

值之間的過渡

示範

三次貝茲曲線:0.00

自訂函式:0.00

向量:[0.00, 0.00]

用法

定義要追蹤的數值來源值,當值變更時,輸出將過渡到新值。如果來源在過渡過程中變更,新的過渡將從先前的過渡中斷處開始。

js
import { TransitionPresets, useTransition } from '@vueuse/core'
import { shallowRef } from 'vue'

const source = shallowRef(0)

const output = useTransition(source, {
  duration: 1000,
  transition: TransitionPresets.easeInOutCubic,
})

若要同步過渡,請使用數字陣列。例如,以下是如何在顏色之間過渡。

js
const source = shallowRef([0, 0, 0])

const output = useTransition(source)

const color = computed(() => {
  const [r, g, b] = output.value
  return `rgb(${r}, ${g}, ${b})`
})

可以使用三次貝茲曲線自訂過渡緩和效果。以這種方式定義的過渡與 CSS 緩和函式 的運作方式相同。

js
useTransition(source, {
  transition: [0.75, 0, 0.25, 1],
})

以下過渡可透過 TransitionPresets 常數取得。

對於更複雜的過渡,可以提供自訂函式。

js
function easeOutElastic(n) {
  return n === 0
    ? 0
    : n === 1
      ? 1
      : (2 ** (-10 * n)) * Math.sin((n * 10 - 0.75) * ((2 * Math.PI) / 3)) + 1
}

useTransition(source, {
  transition: easeOutElastic,
})

若要控制過渡何時開始,請設定 delay 值。若要在過渡前後編排行為,請定義 onStartedonFinished 回呼。

js
useTransition(source, {
  delay: 1000,
  onStarted() {
    // called after the transition starts
  },
  onFinished() {
    // called after the transition ends
  },
})

若要暫時停止過渡,請定義布林值 disabled 屬性。請注意,這與 duration0 不同。停用的過渡會同步追蹤來源值。它們不遵守 delay,也不會觸發 onStartedonFinished 回呼。

為了更精細的控制,可以使用 executeTransition 手動執行過渡。此函式會傳回一個 Promise,該 Promise 在完成時解析。可以透過定義傳回真值的 abort 函式來取消手動過渡。

js
import { executeTransition } from '@vueuse/core'

await executeTransition(source, from, to, {
  duration: 1000,
})

類型宣告

顯示類型宣告
typescript
/**
 * Cubic bezier points
 */
export type CubicBezierPoints = [number, number, number, number]
/**
 * Easing function
 */
export type EasingFunction = (n: number) => number
/**
 * Transition options
 */
export interface TransitionOptions {
  /**
   * Manually abort a transition
   */
  abort?: () => any
  /**
   * Transition duration in milliseconds
   */
  duration?: MaybeRef<number>
  /**
   * Easing function or cubic bezier points for calculating transition values
   */
  transition?: MaybeRef<EasingFunction | CubicBezierPoints>
}
export interface UseTransitionOptions extends TransitionOptions {
  /**
   * Milliseconds to wait before starting transition
   */
  delay?: MaybeRef<number>
  /**
   * Disables the transition
   */
  disabled?: MaybeRef<boolean>
  /**
   * Callback to execute after transition finishes
   */
  onFinished?: () => void
  /**
   * Callback to execute after transition starts
   */
  onStarted?: () => void
}
declare const _TransitionPresets: {
  readonly easeInSine: readonly [0.12, 0, 0.39, 0]
  readonly easeOutSine: readonly [0.61, 1, 0.88, 1]
  readonly easeInOutSine: readonly [0.37, 0, 0.63, 1]
  readonly easeInQuad: readonly [0.11, 0, 0.5, 0]
  readonly easeOutQuad: readonly [0.5, 1, 0.89, 1]
  readonly easeInOutQuad: readonly [0.45, 0, 0.55, 1]
  readonly easeInCubic: readonly [0.32, 0, 0.67, 0]
  readonly easeOutCubic: readonly [0.33, 1, 0.68, 1]
  readonly easeInOutCubic: readonly [0.65, 0, 0.35, 1]
  readonly easeInQuart: readonly [0.5, 0, 0.75, 0]
  readonly easeOutQuart: readonly [0.25, 1, 0.5, 1]
  readonly easeInOutQuart: readonly [0.76, 0, 0.24, 1]
  readonly easeInQuint: readonly [0.64, 0, 0.78, 0]
  readonly easeOutQuint: readonly [0.22, 1, 0.36, 1]
  readonly easeInOutQuint: readonly [0.83, 0, 0.17, 1]
  readonly easeInExpo: readonly [0.7, 0, 0.84, 0]
  readonly easeOutExpo: readonly [0.16, 1, 0.3, 1]
  readonly easeInOutExpo: readonly [0.87, 0, 0.13, 1]
  readonly easeInCirc: readonly [0.55, 0, 1, 0.45]
  readonly easeOutCirc: readonly [0, 0.55, 0.45, 1]
  readonly easeInOutCirc: readonly [0.85, 0, 0.15, 1]
  readonly easeInBack: readonly [0.36, 0, 0.66, -0.56]
  readonly easeOutBack: readonly [0.34, 1.56, 0.64, 1]
  readonly easeInOutBack: readonly [0.68, -0.6, 0.32, 1.6]
}
/**
 * Common transitions
 *
 * @see https://easings.net
 */
export declare const TransitionPresets: Record<
  keyof typeof _TransitionPresets,
  CubicBezierPoints
> & {
  linear: EasingFunction
}
/**
 * Transition from one value to another.
 *
 * @param source
 * @param from
 * @param to
 * @param options
 */
export declare function executeTransition<T extends number | number[]>(
  source: Ref<T>,
  from: MaybeRefOrGetter<T>,
  to: MaybeRefOrGetter<T>,
  options?: TransitionOptions,
): PromiseLike<void>
export declare function useTransition(
  source: MaybeRefOrGetter<number>,
  options?: UseTransitionOptions,
): ComputedRef<number>
export declare function useTransition<T extends MaybeRefOrGetter<number>[]>(
  source: [...T],
  options?: UseTransitionOptions,
): ComputedRef<{
  [K in keyof T]: number
}>
export declare function useTransition<T extends MaybeRefOrGetter<number[]>>(
  source: T,
  options?: UseTransitionOptions,
): ComputedRef<number[]>

原始碼

原始碼示範文件

貢獻者

Anthony Fu
Scott Bedard
Anthony Fu
IlyaL
zhiyuanzmj
Alexey Istomin
huodoushigemi
Jelf
wheat
Alex Kozack

更新日誌

v12.8.0 於 2025/3/5
7432f - feat(types): 棄用 MaybeRefMaybeRefOrGetter,改用 Vue 的原生型別 (#4636)
v12.3.0 於 2025/1/2
59f75 - feat(toValue): 棄用來自 @vueuse/sharedtoValue,改用 Vue 的原生型別
v12.0.0-beta.1 於 2024/11/21
0a9ed - feat!: 移除 Vue 2 支援,最佳化捆綁包並清理 (#4349)
v10.1.0 於 2023/4/22
8b330 - fix: 修復非線性過渡函式的回歸問題 (#2973)
v10.0.0-beta.5 於 2023/4/13
cb644 - refactor!: 移除 isFunctionisString 工具
v10.0.0-beta.4 於 2023/4/13
4d757 - feat(types)!: 將 MaybeComputedRef 重新命名為 MaybeRefOrGetter
0a72b - feat(toValue): 將 resolveUnref 重新命名為 toValue
v10.0.0-beta.1 於 2023/3/23
5944e - feat: 支援 MaybeComputedRef (#2871)
v10.0.0-beta.0 於 2023/3/14
08c21 - feat(useSwipe, usePointerSwipe, useTransition): 改善 tree-shaking (#2863)
526d5 - feat: 公開過渡工具以進行手動控制 (#2743)

在 MIT 許可證下發布。