过渡效果

流畅的动画有助于给用户界面带来很棒的感觉。使用 Vue 的过渡系统和可复用的功能组件,你可以轻松地控制应用程序的动画效果。大多数组件可以通过 transition 属性来改变他们的过渡效果。

用例

Vuetify 附带了 10 多个自定义 css 动画,可应用于众多组件或你自己的自定义的组件。

template


  1. <template>
  2. <v-row justify="center">
  3. <v-menu transition="slide-x-transition">
  4. <template v-slot:activator="{ on }">
  5. <v-btn color="primary" class="ma-2" v-on="on">
  6. Slide X Transition
  7. </v-btn>
  8. </template>
  9. <v-list>
  10. <v-list-item
  11. v-for="n in 5"
  12. :key="n"
  13. link
  14. >
  15. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  16. </v-list-item>
  17. </v-list>
  18. </v-menu>
  19. <div class="mx-6 hidden-sm-and-down"></div>
  20. <v-menu transition="scroll-y-transition">
  21. <template v-slot:activator="{ on }">
  22. <v-btn color="secondary" class="ma-2" v-on="on">
  23. Scroll Y Transition
  24. </v-btn>
  25. </template>
  26. <v-list>
  27. <v-list-item
  28. v-for="n in 5"
  29. :key="n"
  30. link
  31. >
  32. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  33. </v-list-item>
  34. </v-list>
  35. </v-menu>
  36. </v-row>
  37. </template>

Transitions(过渡动画) - 图1

API

从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。

Transitions(过渡动画) - 图2

示例

下面是一些简单到复杂的例子。

X 轴滑动过渡

X 轴滑动过渡可沿水平方向移动。

template


  1. <template>
  2. <v-row
  3. justify="center"
  4. >
  5. <v-menu transition="slide-x-transition">
  6. <template v-slot:activator="{ on }">
  7. <v-btn color="primary" class="ma-2" v-on="on">
  8. Slide X Transition
  9. </v-btn>
  10. </template>
  11. <v-list>
  12. <v-list-item v-for="n in 5" :key="n" link>
  13. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  14. </v-list-item>
  15. </v-list>
  16. </v-menu>
  17. <div class="mx-4 hidden-sm-and-down"></div>
  18. <v-menu transition="slide-x-reverse-transition">
  19. <template v-slot:activator="{ on }">
  20. <v-btn color="secondary" class="ma-2" v-on="on">
  21. Slide X Reverse Transition
  22. </v-btn>
  23. </template>
  24. <v-list>
  25. <v-list-item v-for="n in 5" :key="n" link>
  26. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  27. </v-list-item>
  28. </v-list>
  29. </v-menu>
  30. </v-row>
  31. </template>

Transitions(过渡动画) - 图3

Y 轴滑动过渡

动画使用应用程序的 $primary-transition

template


  1. <template>
  2. <v-row
  3. justify="center"
  4. >
  5. <v-menu transition="slide-y-transition">
  6. <template v-slot:activator="{ on }">
  7. <v-btn color="primary" class="ma-2" v-on="on">
  8. Slide Y Transition
  9. </v-btn>
  10. </template>
  11. <v-list>
  12. <v-list-item v-for="n in 5" :key="n" link>
  13. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  14. </v-list-item>
  15. </v-list>
  16. </v-menu>
  17. <div class="mx-4 hidden-sm-and-down"></div>
  18. <v-menu transition="slide-y-reverse-transition">
  19. <template v-slot:activator="{ on }">
  20. <v-btn color="secondary" class="ma-2" v-on="on">
  21. Slide Y Reverse Transition
  22. </v-btn>
  23. </template>
  24. <v-list>
  25. <v-list-item v-for="n in 5" :key="n" link>
  26. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  27. </v-list-item>
  28. </v-list>
  29. </v-menu>
  30. </v-row>
  31. </template>

Transitions(过渡动画) - 图4

X 轴滚动过渡

X 轴滚动过渡沿着水平轴继续。

template


  1. <template>
  2. <v-row
  3. justify="center"
  4. >
  5. <v-menu transition="scroll-x-transition">
  6. <template v-slot:activator="{ on }">
  7. <v-btn color="primary" class="ma-2" v-on="on">
  8. Scroll X Transition
  9. </v-btn>
  10. </template>
  11. <v-list>
  12. <v-list-item v-for="n in 5" :key="n" link>
  13. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  14. </v-list-item>
  15. </v-list>
  16. </v-menu>
  17. <div class="mx-4 hidden-sm-and-down"></div>
  18. <v-menu transition="scroll-x-reverse-transition">
  19. <template v-slot:activator="{ on }">
  20. <v-btn color="secondary" class="ma-2" v-on="on">
  21. Scroll X Reverse Transition
  22. </v-btn>
  23. </template>
  24. <v-list>
  25. <v-list-item v-for="n in 5" :key="n" link>
  26. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  27. </v-list-item>
  28. </v-list>
  29. </v-menu>
  30. </v-row>
  31. </template>

Transitions(过渡动画) - 图5

Y 轴滚动过渡

Y 轴滚动过渡沿着垂直轴继续。

template


  1. <template>
  2. <v-row
  3. justify="center"
  4. >
  5. <v-menu transition="scroll-y-transition">
  6. <template v-slot:activator="{ on }">
  7. <v-btn color="primary" class="ma-2" v-on="on">
  8. Scroll Y Transition
  9. </v-btn>
  10. </template>
  11. <v-list>
  12. <v-list-item v-for="n in 5" :key="n" link>
  13. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  14. </v-list-item>
  15. </v-list>
  16. </v-menu>
  17. <div class="mx-4 hidden-sm-and-down"></div>
  18. <v-menu transition="scroll-y-reverse-transition">
  19. <template v-slot:activator="{ on }">
  20. <v-btn color="secondary" class="ma-2" v-on="on">
  21. Scroll Y Reverse Transition
  22. </v-btn>
  23. </template>
  24. <v-list>
  25. <v-list-item v-for="n in 5" :key="n" link>
  26. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  27. </v-list-item>
  28. </v-list>
  29. </v-menu>
  30. </v-row>
  31. </template>

Transitions(过渡动画) - 图6

缩放过渡

许多 Vuetify 组件都包含一个 transition 属性允许你指定想要的效果。

template


  1. <template>
  2. <div class="text-center">
  3. <v-menu transition="scale-transition">
  4. <template v-slot:activator="{ on }">
  5. <v-btn
  6. dark
  7. color="primary"
  8. v-on="on"
  9. >
  10. Scale Transition
  11. </v-btn>
  12. </template>
  13. <v-list>
  14. <v-list-item v-for="n in 5" :key="n" @click="">
  15. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  16. </v-list-item>
  17. </v-list>
  18. </v-menu>
  19. </div>
  20. </template>

Transitions(过渡动画) - 图7

Charcoal Vuetify Beanie

Stay warm with the new Premium Knit Vuetify Beanie available in black, navy and charcoal.

ads by Vuetify

](https://store.vuetifyjs.com/product/premium-charcoal-vuetify-beanie?ref=vuetifyjs.com)

Fab 过渡

v-speed-dial 组件中可以找到动画过渡的例子。

template


  1. <template>
  2. <div class="text-center">
  3. <v-menu transition="fab-transition">
  4. <template v-slot:activator="{ on }">
  5. <v-btn
  6. dark
  7. color="primary"
  8. v-on="on"
  9. >
  10. Fab Transition
  11. </v-btn>
  12. </template>
  13. <v-list>
  14. <v-list-item v-for="n in 5" :key="n" @click="">
  15. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  16. </v-list-item>
  17. </v-list>
  18. </v-menu>
  19. </div>
  20. </template>

Transitions(过渡动画) - 图8

淡入淡出过渡

在 Carousel 组件上可以找到淡入淡出的过渡示例。

template


  1. <template>
  2. <div class="text-center">
  3. <v-menu transition="fade-transition">
  4. <template v-slot:activator="{ on }">
  5. <v-btn
  6. dark
  7. color="primary"
  8. v-on="on"
  9. >
  10. Fade Transition
  11. </v-btn>
  12. </template>
  13. <v-list>
  14. <v-list-item v-for="n in 5" :key="n" @click="">
  15. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  16. </v-list-item>
  17. </v-list>
  18. </v-menu>
  19. </div>
  20. </template>

Transitions(过渡动画) - 图9

展开过渡

扩展过渡用于扩展面板和列表组中。也有一个横向版本的 v-expand-x-transition

template script


  1. <template>
  2. <v-row
  3. justify="center"
  4. style="min-height: 160px;"
  5. >
  6. <v-col class="shrink">
  7. <v-btn
  8. class="ma-2"
  9. color="primary"
  10. @click="expand = !expand"
  11. >
  12. Expand Transition
  13. </v-btn>
  14. <v-expand-transition>
  15. <v-card
  16. v-show="expand"
  17. height="100"
  18. width="100"
  19. class="mx-auto"
  20. ></v-card>
  21. </v-expand-transition>
  22. </v-col>
  23. <div class="mx-4 hidden-sm-and-down"></div>
  24. <v-col class="shrink">
  25. <v-btn
  26. class="ma-2"
  27. color="secondary"
  28. @click="expand2 = !expand2"
  29. >
  30. Expand X Transition
  31. </v-btn>
  32. <v-expand-x-transition>
  33. <v-card
  34. v-show="expand2"
  35. height="100"
  36. width="100"
  37. class="mx-auto"
  38. ></v-card>
  39. </v-expand-x-transition>
  40. </v-col>
  41. </v-row>
  42. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. expand: false,
  5. expand2: false,
  6. }),
  7. }
  8. </script>

Transitions(过渡动画) - 图10

自定义原点

通过使用一个简单的属性来可编程地控制过渡原点。

template


  1. <template>
  2. <div class="text-center">
  3. <v-menu transition="scale-transition" origin="center center">
  4. <template v-slot:activator="{ on }">
  5. <v-btn
  6. dark
  7. color="primary"
  8. v-on="on"
  9. >
  10. Scale Transition
  11. </v-btn>
  12. </template>
  13. <v-list>
  14. <v-list-item v-for="n in 5" :key="n" @click="() => {}">
  15. <v-list-item-title v-text="'Item ' + n"></v-list-item-title>
  16. </v-list-item>
  17. </v-list>
  18. </v-menu>
  19. </div>
  20. </template>

Transitions(过渡动画) - 图11

Todo 列表

使用多个自定义转场,可以轻松实现简单的待办事项清单!

template script


  1. <template>
  2. <v-container style="max-width: 500px">
  3. <v-text-field
  4. v-model="task"
  5. label="What are you working on?"
  6. solo
  7. @keydown.enter="create"
  8. >
  9. <v-fade-transition v-slot:append>
  10. <v-icon
  11. v-if="task"
  12. @click="create"
  13. >
  14. add_circle
  15. </v-icon>
  16. </v-fade-transition>
  17. </v-text-field>
  18. <h2 class="display-1 success--text pl-4">
  19. Tasks:&nbsp;
  20. <v-fade-transition leave-absolute>
  21. <span :key="`tasks-${tasks.length}`">
  22. {{ tasks.length }}
  23. </span>
  24. </v-fade-transition>
  25. </h2>
  26. <v-divider class="mt-4"></v-divider>
  27. <v-row
  28. class="my-1"
  29. align="center"
  30. >
  31. <strong class="mx-4 info--text text--darken-2">
  32. Remaining: {{ remainingTasks }}
  33. </strong>
  34. <v-divider vertical></v-divider>
  35. <strong class="mx-4 success--text text--darken-2">
  36. Completed: {{ completedTasks }}
  37. </strong>
  38. <v-spacer></v-spacer>
  39. <v-progress-circular
  40. :value="progress"
  41. class="mr-2"
  42. ></v-progress-circular>
  43. </v-row>
  44. <v-divider class="mb-4"></v-divider>
  45. <v-card v-if="tasks.length > 0">
  46. <v-slide-y-transition
  47. class="py-0"
  48. group
  49. tag="v-list"
  50. >
  51. <template v-for="(task, i) in tasks">
  52. <v-divider
  53. v-if="i !== 0"
  54. :key="`${i}-divider`"
  55. ></v-divider>
  56. <v-list-item :key="`${i}-${task.text}`">
  57. <v-list-item-action>
  58. <v-checkbox
  59. v-model="task.done"
  60. :color="task.done && 'grey' || 'primary'"
  61. >
  62. <template v-slot:label>
  63. <div
  64. :class="task.done && 'grey--text' || 'primary--text'"
  65. class="ml-4"
  66. v-text="task.text"
  67. ></div>
  68. </template>
  69. </v-checkbox>
  70. </v-list-item-action>
  71. <v-spacer></v-spacer>
  72. <v-scroll-x-transition>
  73. <v-icon
  74. v-if="task.done"
  75. color="success"
  76. >
  77. check
  78. </v-icon>
  79. </v-scroll-x-transition>
  80. </v-list-item>
  81. </template>
  82. </v-slide-y-transition>
  83. </v-card>
  84. </v-container>
  85. </template>
  1. <script>
  2. export default {
  3. data: () => ({
  4. tasks: [
  5. {
  6. done: false,
  7. text: 'Foobar',
  8. },
  9. {
  10. done: false,
  11. text: 'Fizzbuzz',
  12. },
  13. ],
  14. task: null,
  15. }),
  16. computed: {
  17. completedTasks () {
  18. return this.tasks.filter(task => task.done).length
  19. },
  20. progress () {
  21. return this.completedTasks / this.tasks.length * 100
  22. },
  23. remainingTasks () {
  24. return this.tasks.length - this.completedTasks
  25. },
  26. },
  27. methods: {
  28. create () {
  29. this.tasks.push({
  30. done: false,
  31. text: this.task,
  32. })
  33. this.task = null
  34. },
  35. },
  36. }
  37. </script>

Transitions(过渡动画) - 图12

创建你自己的

你可以使用 Vuetify 的助手函数来创建你自己的自定义的过渡效果,这个函数会返回一个可以导入到 Vue 的对象。使用 Vue functional component 配置将确保您的过渡效果尽可能高性能。只需要导入函数:

  1. import { createSimpleTransition } from 'vuetify/lib/components/transitions/createTransition'
  2. // ES5
  3. // import { createSimpleTransition } from 'vuetify/es5/components/transitions/createTransition'
  4. const myTransition = createSimpleTransition('my-transition')
  5. Vue.component('my-transition', myTransition)

其中 createSimpleTransition 函数接受 1 个参数,即 name。这将是你可以与你的样式挂钩的名称。这是一个展示 v-fade-transition 的示例:

  1. .fade-transition
  2. &-leave-active
  3. position: absolute
  4. &-enter-active, &-leave, &-leave-to
  5. transition: $primary-transition
  6. &-enter, &-leave-to
  7. opacity: 0