他们的优先级高于普通的类名,
这对于 Vue 的过渡系统和其他第
三方 CSS 动画库,如 [Animate.css]
(https://daneden.github.io/animate.css/) 结合使用十分有用。
示例:
<link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
<div id="example-3">
<button @click="show = !show">
Toggle render
</button>
<transition
name="custom-classes-transition"
enter-active-class="animated tada"
leave-active-class="animated bounceOutRight"
>
<p v-if="show">hello</p>
</transition>
</div>
单元素过渡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
/* Vue 的过渡系统 */
.fade-enter-active, .fade-leave-active {
transition: opacity .5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
opacity: 0;
}
/* 自定义css过渡动画 */
.kerwinbounce-entet-active {
animation: bounce-in .5s;
}
.kerwinbounce-leave-active {
animation: bounce-in .5s reverse;
}
@keyframes bounce-in {
0% {
opacity: 0;
transform: translateX(100px);
}
100% {
opacity: 1;
transform: translateX(0px);
}
}
</style>
<!-- 引入Animate.css库 -->
<link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
<script src="../vue.js"></script>
<div id="app">
<button @click="handleClick">click</button>
<!-- Vue 的过渡系统 -->
<transition name="fade">
<p v-show="isShow">test</p>
</transition>
<!-- 自定义css过渡动画 -->
<transition name="kerwinbounce">
<p v-show="isShow">test</p>
</transition>
<!-- Vue 的过渡系统和Animate.css结合 -->
<transition
name="custom-classes-transition"
enter-active-class="animated tada"
leave-active-class="animated bounceOutRight"
>
<p v-show="isShow">test</p>
</transition>
<h1 class="animated infinite bounce delay-2s">Example</h1>
</div>
<script>
new Vue({
el: "#app",
data: {
isShow: true
},
methods: {
handleClick() {
this.isShow = !this.isShow
}
},
})
</script>
</body>
</html>
多元素过渡
当有相同标签名的元素切换时,需要通过key特性设置唯一的值来标记以让Vue区分它们,否则为了效率只会替换相同标签内部内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 自定义css过渡动画 */
.kerwinbounce-entet-active {
animation: bounce-in .5s;
}
.kerwinbounce-leave-active {
animation: bounce-in .5s reverse;
}
@keyframes bounce-in {
0% {
opacity: 0;
transform: translateX(100px);
}
100% {
opacity: 1;
transform: translateX(0px);
}
}
</style>
</head>
<body>
<script src="../vue.js"></script>
<div id="app">
<button @click="handleClick">Click</button>
<!-- 设置key -->
<!-- 当有相同标签名的元素切换时,需要通过key特性设置唯一的值来标记以让Vue区分它们,否则为了效率只会替换相同标签内部内容 -->
<!-- mode in-out out-in -->
<transition name="kerwinbounce" mode="in-out">
<p v-if="isShow" key="1">11111111</p>
<p v-else key="2">222222222</p>
</transition>
</div>
<script>
new Vue({
el: "#app",
data: {
isShow: true
},
methods: {
handleClick() {
this.isShow = !this.isShow
}
},
})
</script>
</body>
</html>
案例:动态组件+多元素过渡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
*{padding: 0;margin: 0;}
ul{
display: flex;
list-style: none;
position: absolute;
bottom: 0;
left: 0;
right: 0;
justify-content:space-between;
height: 30px;
background-color: grey;
}
li{
background: royalblue;
width: 33%;
display: flex;
align-items: center;
justify-content: center;
}
/* 自定义css过渡动画 */
.kerwinbounce-entet-active {
animation: bounce-in .5s;
}
.kerwinbounce-leave-active {
animation: bounce-in .5s reverse;
}
@keyframes bounce-in {
0% {
opacity: 0;
transform: translateX(100px);
}
100% {
opacity: 1;
transform: translateX(0px);
}
}
</style>
<script src="../vue.js"></script>
<div id="app">
<!-- <keep-alive>标签:保留状态避免重新渲染-->
<!-- <component>标签:元素,动态地绑定多个组件到它的is属性 -->
<keep-alive>
<transition name="kerwinbounce" mode="out-in">
<component :is="isShow"></component>
</transition>
</keep-alive>
<ul>
<li @click="handleHomeClick">首页</li>
<li @click="handleListClick">列表页</li>
<li @click="handleShopcarClick">购物车页</li>
</ul>
</div>
<script>
new Vue({
el: "#app",
components: {//局部组件
home:{
template:`<div>home<input type="text"></div>`
},
list:{
template: `<div>list</div>`
},
shopcar:{
template: `<div>shopcar</div>`
},
},
data: {
isShow:"home"
},
methods: {
// 改变 is属性
handleHomeClick() {
this.isShow = "home"
},
handleListClick() {
this.isShow = "list"
},
handleShopcarClick() {
this.isShow = "shopcar"
}
},
})
</script>
</body>
</html>