过渡效果
流畅的动画有助于给用户界面带来很棒的感觉。使用 Vue 的过渡系统和可复用的功能组件,你可以轻松地控制应用程序的动画效果。大多数组件可以通过 transition
属性来改变他们的过渡效果。
用例
Vuetify 附带了 10 多个自定义 css 动画,可应用于众多组件或你自己的自定义的组件。
template
<template>
<v-row justify="center">
<v-menu transition="slide-x-transition">
<template v-slot:activator="{ on }">
<v-btn color="primary" class="ma-2" v-on="on">
Slide X Transition
</v-btn>
</template>
<v-list>
<v-list-item
v-for="n in 5"
:key="n"
link
>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
<div class="mx-6 hidden-sm-and-down"></div>
<v-menu transition="scroll-y-transition">
<template v-slot:activator="{ on }">
<v-btn color="secondary" class="ma-2" v-on="on">
Scroll Y Transition
</v-btn>
</template>
<v-list>
<v-list-item
v-for="n in 5"
:key="n"
link
>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-row>
</template>
API
从下面选择您想要的组件,并查看可用的属性、插槽、事件和函数。
示例
下面是一些简单到复杂的例子。
X 轴滑动过渡
X 轴滑动过渡可沿水平方向移动。
template
<template>
<v-row
justify="center"
>
<v-menu transition="slide-x-transition">
<template v-slot:activator="{ on }">
<v-btn color="primary" class="ma-2" v-on="on">
Slide X Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" link>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
<div class="mx-4 hidden-sm-and-down"></div>
<v-menu transition="slide-x-reverse-transition">
<template v-slot:activator="{ on }">
<v-btn color="secondary" class="ma-2" v-on="on">
Slide X Reverse Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" link>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-row>
</template>
Y 轴滑动过渡
动画使用应用程序的 $primary-transition
。
template
<template>
<v-row
justify="center"
>
<v-menu transition="slide-y-transition">
<template v-slot:activator="{ on }">
<v-btn color="primary" class="ma-2" v-on="on">
Slide Y Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" link>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
<div class="mx-4 hidden-sm-and-down"></div>
<v-menu transition="slide-y-reverse-transition">
<template v-slot:activator="{ on }">
<v-btn color="secondary" class="ma-2" v-on="on">
Slide Y Reverse Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" link>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-row>
</template>
X 轴滚动过渡
X 轴滚动过渡沿着水平轴继续。
template
<template>
<v-row
justify="center"
>
<v-menu transition="scroll-x-transition">
<template v-slot:activator="{ on }">
<v-btn color="primary" class="ma-2" v-on="on">
Scroll X Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" link>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
<div class="mx-4 hidden-sm-and-down"></div>
<v-menu transition="scroll-x-reverse-transition">
<template v-slot:activator="{ on }">
<v-btn color="secondary" class="ma-2" v-on="on">
Scroll X Reverse Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" link>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-row>
</template>
Y 轴滚动过渡
Y 轴滚动过渡沿着垂直轴继续。
template
<template>
<v-row
justify="center"
>
<v-menu transition="scroll-y-transition">
<template v-slot:activator="{ on }">
<v-btn color="primary" class="ma-2" v-on="on">
Scroll Y Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" link>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
<div class="mx-4 hidden-sm-and-down"></div>
<v-menu transition="scroll-y-reverse-transition">
<template v-slot:activator="{ on }">
<v-btn color="secondary" class="ma-2" v-on="on">
Scroll Y Reverse Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" link>
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</v-row>
</template>
缩放过渡
许多 Vuetify 组件都包含一个 transition
属性允许你指定想要的效果。
template
<template>
<div class="text-center">
<v-menu transition="scale-transition">
<template v-slot:activator="{ on }">
<v-btn
dark
color="primary"
v-on="on"
>
Scale Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" @click="">
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
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
<template>
<div class="text-center">
<v-menu transition="fab-transition">
<template v-slot:activator="{ on }">
<v-btn
dark
color="primary"
v-on="on"
>
Fab Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" @click="">
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
淡入淡出过渡
在 Carousel 组件上可以找到淡入淡出的过渡示例。
template
<template>
<div class="text-center">
<v-menu transition="fade-transition">
<template v-slot:activator="{ on }">
<v-btn
dark
color="primary"
v-on="on"
>
Fade Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" @click="">
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
展开过渡
扩展过渡用于扩展面板和列表组中。也有一个横向版本的 v-expand-x-transition
。
template script
<template>
<v-row
justify="center"
style="min-height: 160px;"
>
<v-col class="shrink">
<v-btn
class="ma-2"
color="primary"
@click="expand = !expand"
>
Expand Transition
</v-btn>
<v-expand-transition>
<v-card
v-show="expand"
height="100"
width="100"
class="mx-auto"
></v-card>
</v-expand-transition>
</v-col>
<div class="mx-4 hidden-sm-and-down"></div>
<v-col class="shrink">
<v-btn
class="ma-2"
color="secondary"
@click="expand2 = !expand2"
>
Expand X Transition
</v-btn>
<v-expand-x-transition>
<v-card
v-show="expand2"
height="100"
width="100"
class="mx-auto"
></v-card>
</v-expand-x-transition>
</v-col>
</v-row>
</template>
<script>
export default {
data: () => ({
expand: false,
expand2: false,
}),
}
</script>
自定义原点
通过使用一个简单的属性来可编程地控制过渡原点。
template
<template>
<div class="text-center">
<v-menu transition="scale-transition" origin="center center">
<template v-slot:activator="{ on }">
<v-btn
dark
color="primary"
v-on="on"
>
Scale Transition
</v-btn>
</template>
<v-list>
<v-list-item v-for="n in 5" :key="n" @click="() => {}">
<v-list-item-title v-text="'Item ' + n"></v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
</template>
Todo 列表
使用多个自定义转场,可以轻松实现简单的待办事项清单!
template script
<template>
<v-container style="max-width: 500px">
<v-text-field
v-model="task"
label="What are you working on?"
solo
@keydown.enter="create"
>
<v-fade-transition v-slot:append>
<v-icon
v-if="task"
@click="create"
>
add_circle
</v-icon>
</v-fade-transition>
</v-text-field>
<h2 class="display-1 success--text pl-4">
Tasks:
<v-fade-transition leave-absolute>
<span :key="`tasks-${tasks.length}`">
{{ tasks.length }}
</span>
</v-fade-transition>
</h2>
<v-divider class="mt-4"></v-divider>
<v-row
class="my-1"
align="center"
>
<strong class="mx-4 info--text text--darken-2">
Remaining: {{ remainingTasks }}
</strong>
<v-divider vertical></v-divider>
<strong class="mx-4 success--text text--darken-2">
Completed: {{ completedTasks }}
</strong>
<v-spacer></v-spacer>
<v-progress-circular
:value="progress"
class="mr-2"
></v-progress-circular>
</v-row>
<v-divider class="mb-4"></v-divider>
<v-card v-if="tasks.length > 0">
<v-slide-y-transition
class="py-0"
group
tag="v-list"
>
<template v-for="(task, i) in tasks">
<v-divider
v-if="i !== 0"
:key="`${i}-divider`"
></v-divider>
<v-list-item :key="`${i}-${task.text}`">
<v-list-item-action>
<v-checkbox
v-model="task.done"
:color="task.done && 'grey' || 'primary'"
>
<template v-slot:label>
<div
:class="task.done && 'grey--text' || 'primary--text'"
class="ml-4"
v-text="task.text"
></div>
</template>
</v-checkbox>
</v-list-item-action>
<v-spacer></v-spacer>
<v-scroll-x-transition>
<v-icon
v-if="task.done"
color="success"
>
check
</v-icon>
</v-scroll-x-transition>
</v-list-item>
</template>
</v-slide-y-transition>
</v-card>
</v-container>
</template>
<script>
export default {
data: () => ({
tasks: [
{
done: false,
text: 'Foobar',
},
{
done: false,
text: 'Fizzbuzz',
},
],
task: null,
}),
computed: {
completedTasks () {
return this.tasks.filter(task => task.done).length
},
progress () {
return this.completedTasks / this.tasks.length * 100
},
remainingTasks () {
return this.tasks.length - this.completedTasks
},
},
methods: {
create () {
this.tasks.push({
done: false,
text: this.task,
})
this.task = null
},
},
}
</script>
创建你自己的
你可以使用 Vuetify 的助手函数来创建你自己的自定义的过渡效果,这个函数会返回一个可以导入到 Vue 的对象。使用 Vue functional component 配置将确保您的过渡效果尽可能高性能。只需要导入函数:
import { createSimpleTransition } from 'vuetify/lib/components/transitions/createTransition'
// ES5
// import { createSimpleTransition } from 'vuetify/es5/components/transitions/createTransition'
const myTransition = createSimpleTransition('my-transition')
Vue.component('my-transition', myTransition)
其中 createSimpleTransition
函数接受 1 个参数,即 name。这将是你可以与你的样式挂钩的名称。这是一个展示 v-fade-transition
的示例:
.fade-transition
&-leave-active
position: absolute
&-enter-active, &-leave, &-leave-to
transition: $primary-transition
&-enter, &-leave-to
opacity: 0