vue-transitions
@morev/vue-transitions

可複用的介面過渡效果,無需 CSS ❤️

Promo image of @morev/vue-transitions package

Stability of "master" branchLicense: MITLast commitRelease versionGitHub Release DateKeywords

@morev/vue-transitions

適用於 Vue 2Vue 3 的可複用介面過渡效果,無需 CSS ❤️

✔️ 可透過 props 高度自定義;
✔️ 在 group 模式下與 grid/flex 佈局正確配合;
✔️ 考慮動畫元素的初始樣式,例如 transformopacity
✔️ 搭配通用的 Nuxt 2Nuxt 3 模組,使用起來更加方便。

DEMO / 演練場

目錄

安裝

❗ 要求

  • Node 版本:>= 18.0.0
  • Nuxt 版本(如果使用):>= 2.17.0 || >= 3.5.0

如果您使用的 Node 或 Nuxt 版本低於指定版本,該外掛將無法工作。


使用 yarn

yarn add @morev/vue-transitions

使用 npm

npm install @morev/vue-transitions

使用 pnpm

pnpm add @morev/vue-transitions

使用 bun

bun add @morev/vue-transitions

Bun 使用者重要提示

該軟體包依賴於 postinstall 鉤子來確定 Vue 版本並提供適當的元件。
預設情況下,Bun 不執行生命週期鉤子,因此要使其工作,您需要在安裝後手動將軟體包新增到 trustedDependencies 中,然後再次執行 bun install

{
  "trustedDependencies": ["@morev/vue-transitions"]
}

使用

如果您打算將庫與 Nuxt 一起使用,可以跳過以下段落。
前往“Nuxt 用法”部分.

包匯出兩個版本的元件

  • Vue2 版本可透過命名匯出 /vue2 獲得
  • Vue3 版本可透過命名匯出 /vue3 獲得

但是,還有一個預設匯出對映到所使用的本地 Vue 版本。
在底層,它利用了 postinstall npm 鉤子。
安裝包後,指令碼將開始檢查已安裝的 Vue 版本並根據本地 Vue 版本重定向匯出。

它感覺非常健壯,但如果您擔心,建議根據您使用的版本進行顯式命名匯入。

順便說一句,您可以在安裝後更改預設匯出:只需執行命令 vue-transitions-version-switch <version>

  • 使用 yarn 的示例:yarn vue-transitions-version-switch 2
  • 使用 npx 的示例:npx vue-transitions-version-switch 3

全域性註冊

使用 Vue3

import { createApp } from 'vue';
import { plugin as vueTransitionsPlugin } from '@morev/vue-transitions';
import '@morev/vue-transitions/styles';

const app = createApp(App);

app.use(vueTransitionsPlugin({
  // Plugin options (optional, described below)
}));

使用 Vue2

import Vue from 'vue';
import { plugin as vueTransitionsPlugin } from '@morev/vue-transitions';
import '@morev/vue-transitions/styles';

Vue.use(vueTransitionsPlugin, {
  // Plugin options (optional, described below)
});
😥 我收到了錯誤“未找到此依賴項”

對於無法解析 exports 欄位的環境(例如 Nuxt 2),只需將樣式匯入替換為檔案的直接路徑

- import '@morev/vue-transitions/styles';
+ import '@morev/vue-transitions/dist/index.css';

自定義選項

建議在設定自定義選項時使用命名匯出 plugin,而不是預設匯出,以獲得正確的型別資訊。

自定義選項允許更改預設屬性值。
要更改預設值,請將 defaultProps 鍵傳遞給外掛設定,列出所需屬性的鍵值對。
您也可以為每個元件更改預設屬性,只需將 componentDefaultProps 鍵傳遞給外掛設定即可。

重要:這些屬性未經驗證,因此請確保使用正確的值定義它們。

import { createApp } from 'vue';
import { plugin as vueTransitionsPlugin } from '@morev/vue-transitions';
import '@morev/vue-transitions/styles';

const app = createApp(App);

app.use(vueTransitionsPlugin({
  // Default duration for all transitions now is `200`
  defaultProps: {
    duration: 200,
  },
  // But for `<transition-expand>` default duration is `500`
  componentDefaultProps: {
    TransitionExpand: {
      duration: 500,
    }
  }
}));

直接匯入元件

<template>
  <transition-fade>
    <div v-if="isVisible" class="box">
      Fade transition
    </div>
  </transition-fade>
</template>

<script>
  import { TransitionFade } from '@morev/vue-transitions';

  export default {
    components: { TransitionFade },
  };
</script>

Nuxt 用法

該庫透過命名匯出 /nuxt 匯出了一個適用於 Nuxt 2 和 3 的即用型通用模組。
使用 Nuxt 時,建議使用模組而不是手動安裝,因為

  1. Nuxt 允許按需自動匯入元件,而不是全域性註冊,這是一種效能更高的選項。
  2. 這樣做更快:)

要使用,請將 @morev/vue-transitions/nuxt 新增到 nuxt.config.ts / nuxt.config.jsmodules 部分

export default defineNuxtConfig({
  modules: [
    '@morev/vue-transitions/nuxt',
  ],
  vueTransitions: {
    // The same options as in the plugin itself.
    // You will get an autocomplete using Nuxt 3.
  }
});

享受您的過渡元件!🎉

智慧感知

使用 Nuxt 模組時可以跳過此部分 - 它會為您處理。

此部分僅適用於 VSCode 設定和元件的全域性註冊。

使用 Vue 2

安裝了 Vetur 擴充套件的 Vue 2 環境中,所有元件都應提供提示,無需任何操作

Example of IntelliSense with VSCode and Vetur

使用 Vue 3

安裝了 Volar 擴充套件的 Vue 3 環境中,所有元件都應提供提示,無需任何操作

Example of IntelliSense with VSCode and Volar

過渡效果列表

TransitionFade

基本的過渡效果,改變元素 opacity。非常簡單。

顯示程式碼
<template>
  <transition-fade>
    <div v-if="isVisible">...</div>
  </transition-fade>
</template>

<script>
  import { TransitionFade } from '@morev/vue-transitions';

  export default {
    components: { TransitionFade },
  };
</script>

TransitionExpand

操縱實際元素大小的過渡效果。
如果您足夠老,您可能知道這種過渡效果被稱為 jQuery slideUp/slideDown
它也可以沿 X 軸工作,如 slideLeftslideRight(儘管我很難想出它真正需要的場景)。

有一個獨有屬性axis


TransitionSlide

透過 transform: translate() 操縱元素位置的過渡效果。
它需要 offset 屬性來計算所需的元素位置,並且可以與百分比值一起使用。

如何使用 offset 屬性的示例
<template>
  <!--
    Element will fade in and fade out to top.
    Initial transform is `transform: translate(0, -16px)`
  -->
  <transition-slide :offset="[0, -16]"></transition-slide>

  <!--
    Element will fade in and fade out to bottom left side.
    Initial transform is `transform: translate(-16px, 16px)`
  -->
  <transition-slide :offset="[-16, 16]"></transition-slide>

  <!--
    Element will fade in and fade out to right,
    and the offset will be relative to the element width itself.
    Initial transform is `transform: translate(100%, 0)`
  -->
  <transition-slide :offset="['100%', 0]"></transition-slide>

  <!--
    Element will fade in from top and fade out to bottom,
    and the offset will be relative to the element height itself.

    Transform before element appears: `transform: translate(0, -100%)`
    Transform when element disappears: `transform: translate(0, 100%)`
  -->
  <transition-slide
    :offset="{
      enter: [0, '-100%'],
      leave: [0, '100%']
    }"
  ></transition-slide>
</template>

它尊重透過 CSS 應用於元素自身的 transform,並將其與定義的偏移量合併。
這非常有用,例如,當您嘗試製作居中下拉選單時。

👀 顯示 `transform` 合併示例
<template>
  <div class="block">
    <!--
      In this case, given the CSS styles,
      initial transform will be calculated to `translate(-50%, -16px)`
    -->
    <transition-slide :offset="[0, -16]">
      <div class="block__dropdown" v-if="isVisible">
        ...
      </div>
    </transition-slide>
  </div>
</template>

<style>
  .block {
    position: relative;
  }

  .block__dropdown {
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
  }
</style>

有一個獨有屬性offset


TransitionScale

操縱元素 transform: scale() 的過渡效果。
預設情況下,它將元素從 scale(1) 縮放到 scale(0),但此行為可以透過 :scale 屬性進行自定義。
它透過 axis 屬性在不同的軸上工作。

獨有屬性scaleaxisorigin

顯示程式碼示例
<template>
  <!--
    This element appears in `x` axis and disappears in `y`
  -->
  <transition-scale :axis="{ enter: 'x', leave: 'y' }"></transition-scale>

  <!--
    This element behaves like the `transition-expand`,
    but touches only `transform` property
  -->
  <transition-scale transform-origin="50% 0%"></transition-scale>

  <!--
    This element scales just a little when fading in/out.
  -->
  <transition-scale :scale=".8"></transition-scale>

</template>

屬性

通用屬性

這些屬性與每個過渡效果相關

group

元件是否應該是 transition-group 元件。

export type TransitionGroup = boolean; // Default: false

示例

<template>
  <div>
    <!--
      To animate a list of items, use `group` prop.
      ⚠️ Don't forget you should pass the `:key` to each item in this case.
    -->
    <transition-fade group>
      <div v-for="item in items" :key="item.id">...</div>
    </transition-fade>
  </div>
</template>
tag

在使用了 transition-group 元件的情況下,過渡的標籤。

export type TransitionTag = string; // Default: 'span'

示例

<template>
  <div>
    <!--
      Passing the tag renders transition component with it.
      It's suitable, for example, for rendering semantic lists:
    -->
    <transition-fade group tag="ul">
      <li v-for="item in items" :key="item.id">...</li>
    </transition-fade>

    <!-- ✨ Rendered HTML: -->
    <ul>
      <li>...</li>
      <li>...</li>
    </ul>
  </div>
</template>
appear

是否在節點首次渲染時應用過渡。作用與原生的 Vue 過渡 appear 屬性 完全相同

export type TransitionAppear = boolean; // Default: undefined

示例

<template>
  <div>
    <!--
      This element appears when mounted if `isVisible` is `true` by default.
    -->
    <transition-fade appear>
      <div v-if="isVisible">...</div>
    </transition-fade>
  </div>
</template>
模式

過渡模式.

export type TransitionMode = 'in-out' | 'out-in' | undefined; // Default: undefined

示例

<template>
  <div>
    <!--
      Current element transitions out first, then when complete,
      the new element transitions in.
    -->
    <transition-slide mode="out-in">
      <component :is="currentComponent">...</component>
    </transition-slide>
  </div>
</template>
duration

過渡動畫持續時間,單位毫秒。
如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

// Default: 300
export type TransitionDuration = number | { enter: number, leave: number }

示例

<template>
  <div>
    <!--
      If single value provided, the passed amount of milliseconds
      applied to enter and leave animations both.
      This element will appear and disappear within 500ms:
    -->
    <transition-fade :duration="500">
      <div v-if="isVisible">...</div>
    </transition-fade>

    <!--
      If an object given, it SHOULD have `enter` and `leave` keys.
      This element appears in 200ms and disappears within 500ms:
    -->
    <transition-fade :duration="{ enter: 200, leave: 500 }">
      <div v-if="isVisible">...</div>
    </transition-fade>
  </div>
</template>
move-duration

在使用 transition-group 時,元素位置變化的動畫持續時間。

儘管 Vue 沒有透過 props 設定移動動畫持續時間的原生方式,但可以透過使用 CSS 自定義屬性 來完成此任務。

👀 顯示解釋
<!-- This way it can be set dynamically -->
<div style="--move-duration: 300ms;">
  <div class="scale-move"></div>
  <div class="scale-move"></div>
</div>
```

```css
.scale-move {
  transition-duration: var(--move-duration, 300ms);
}
```

</details>

```ts
// Default: 300
export type TransitionMoveDuration = number;
delay

過渡動畫延遲,單位毫秒。
如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

// Default: 300
export type TransitionDelay = number | { enter: number, leave: number };

示例

<template>
  <div>
    <!--
      If single value provided, then enter and leave animations will wait
      for given amount of milliseconds before run.
      This element will appear and disappear 100ms after
      `isVisible` property changes:
    -->
    <transition-fade :delay="100">
      <div v-if="isVisible">...</div>
    </transition-fade>

    <!--
      If an object given, it SHOULD have `enter` and `leave` keys.
      This element appears immediately and disappears 200ms after
      `isVisible` property changes:
    -->
    <transition-fade :duration="{ enter: 0, leave: 300 }">
      <div v-if="isVisible">...</div>
    </transition-fade>
  </div>
</template>
easing

過渡動畫緩動函式。應為有效的 CSS 過渡計時函式。
如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

export type TransitionEasing = string; // Default: 'cubic-bezier(.25, .8, .5, 1)'

示例

<template>
  <div>
    <!--
      If single value provided, then enter and leave animations will use it:
    -->
    <transition-fade easing="ease-out">
      <div v-if="isVisible">...</div>
    </transition-fade>

    <!--
      If an object given, it SHOULD have `enter` and `leave` keys.
      This element uses custom animation known as `bounce-in` for entering
      and simple `ease-out` curve for leaving:
    -->
    <transition-fade
      :easing="{
        enter: 'cubic-bezier(0.6, 0, 0.4, 2)',
        leave: 'ease-out'
      }"
    >
      <div v-if="isVisible">...</div>
    </transition-fade>
  </div>
</template>
no-opacity

是否為元素 opacity 新增動畫。

預設情況下,每個過渡效果除了主要屬性外,還會操縱 opacity
然而,有時這並不是必需的——例如,在實現從螢幕邊緣出現的模態面板時。

該屬性顯然不適用於 transition-fade 元件。

export type TransitionNoOpacity = boolean; // Default: false

示例

<template>
  <div>
    <!--
      This panel appears from the right edge of the screen,
      while its transparency remains unchanged.
    -->
    <transition-slide :offset="['100%', 0]" no-opacity>
      <div class="panel" v-if="isVisible">...</div>
    </transition-slide>
  </div>
</template>

<style>
  .panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    background: #ffffff;
    width: 400px;
  }
</style>
no-move

是否為元素位置變化新增動畫,在使用 transition-group 時。

預設情況下,當使用 group 模式時,當一個元素被移除時,剩餘元素會平滑地改變它們的位置。
它們被賦予絕對定位並脫離文件流,因此父容器的高度會摺疊。

通常這不是問題,但有時——例如,當使用 transition-expand 和順序放置的元素時,它看起來會很粗糙。
透過此選項,您可以在這種情況下實現更令人愉悅的元素行為。

export type TransitionNoMove = boolean; // Default: false

示例

<template>
  <div>
    <!--
      In this case, the height of the parent element (`ul`) changes smoothly.
    -->
    <transition-expand group no-move tag="ul">
      <li v-for="item in items" :key="item.id">...</li>
    </transition-expand>
  </div>
</template>

TransitionExpand 的獨有屬性

axis

元素應沿哪個軸展開。
如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

type ExpandAxisValue = 'x' | 'y'; // Default: 'y'
export type TransitionExpandAxis = ExpandAxisValue | { enter: ExpandAxisValue, leave: ExpandAxisValue }

TransitionSlide 的獨有屬性

offset

過渡之前/之後元素沿 xy 軸的偏移量。
應為整數或百分比值的字串表示形式(例如 '100%')。

數字值被視為 px 偏移量,以 % 符號結尾的字串值被視為 元素寬度/高度的百分比
示例和解釋

如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

type SlideOffsetValue = [number | string, number | string];

// Default: [0, -16]
export type TransitionSlideOffset = SlideOffsetValue | { enter: SlideOffsetValue, leave: SlideOffsetValue }

TransitionScale 的獨有屬性

axis

要動畫的縮放軸。

* `both` (uses `transform: scale()`)
* `x` (uses `transform: scaleX()`)
* `y` (uses `transform: scaleY()`)

示例和解釋

如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

type ScaleAxisValue = 'x' | 'y' | 'both';

// Default: 'both'
export type TransitionScaleAxis = ScaleAxisValue | { enter: ScaleAxisValue, leave: ScaleAxisValue }
origin

應用於元素的 transform-origin CSS 屬性。

如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

示例和解釋

如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

// Default: '50% 50%'
export type TransitionScaleAxis = string | { enter: string, leave: string }
scale

過渡前後元素的縮放值。應為 01 之間的數字。

如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

示例和解釋

如果給定一個物件,則 enterleave 值將分別用於進入和離開過渡。

// Default: 0
export type TransitionScaleScale = number | { enter: number, leave: number }

事件

元件不提供任何特殊事件,但會觸發 所有標準過渡事件

  • before-enter
  • enter
  • after-enter
  • enter-cancelled
  • before-leave
  • leave
  • after-leave
  • enter-leave