元素在显示和隐藏时,实现过滤或者动画的效果。常用的过渡和动画都是使用 CSS 来实现的
在 CSS 中操作 trasition (过滤 )或 animation (动画)达到不同效果
为目标元素添加一个父元素 , 让父元素通过自动应用 class 类名来达到效果
过渡与动画时,会为对应元素动态添加的相关 class 类名:
- xxx-enter :定义显示前的效果。
- xxx-enter-active :定义显示过程的效果。
- xxx-enter-to : 定义显示后的效果。
- xxx-leave : 定义隐藏前的效果。
- xxx-leave-active :定义隐藏过程的效果。
- xxx-leave-to :定义隐藏后的效果。
过滤效果案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* 显示或隐藏的过渡效果 */
.mxg-enter-active, .mxg-leave-active {
transition: opacity 1s; /*过渡:渐变效果持续时长1秒 */
}
/* 显示前或隐藏后的效果 */
.mxg-enter, .mxg-leave-to {
opacity: 0; /* 都是隐藏效果 */
}
/* 可以针对显示和隐藏指定不同的效果 */
/* 显示过渡效果 1秒 */
.meng-enter-active {
transition: all 1s; /*all 所有效果,持续1秒*/
}
/* 隐藏过渡效果 5秒 */
.meng-leave-active {
transition: all 5s; /*all 所有效果,持续5秒*/
}
/* 显示前或隐藏后的效果 */
.meng-enter, .meng-leave-to {
opacity: 0; /* 都是隐藏效果 */
transform: translateX(10px); /*水平方向 移动 10px*/
}
</style>
</head>
<body>
<div id="app1">
<button @click="show = !show">渐变过渡</button>
<transition name="mxg">
<p v-show="show" >mengxuegu</p>
</transition>
</div>
<div id="app2">
<button @click="show = !show">渐变平滑过渡</button>
<transition name="meng">
<p v-show="show" >mengxuegu</p>
</transition>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
new Vue({
el: '#app1',
data: {
show: true
}
})
new Vue({
el: '#app2',
data: {
show: true
}
})
</script>
</body>
</html>
动画效果案例
CSS 动画用法同 CSS 过渡,只不过采用 animation 为指定动画效果
功能实现:
点击按钮后, 文本内容有放大缩小效果
在 vue-02-过渡&动画和指令 目录下创建 02-动画效果.html
注意:官网上面源码有问题,要在
元素上增加样式 style=“display:inline-block;”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* 显示过程中的动画效果 */
.bounce-enter-active {
animation: bounce-in 1s;
}
/* 隐藏过程中的动画效果 */
.bounce-leave-active {
animation: bounce-in 1s reverse;
}
@keyframes bounce-in {
0% { /*持续时长百分比,比如针对1s: 0%代表0秒,50%代表0.5*/
transform: scale(0); /*缩小为0*/
}
50% {
transform: scale(1.5); /*放大1.5倍*/
}
100% {
transform: scale(1); /*原始大小*/
}
}
</style>
</head>
<body>
<div id="demo">
<button @click="show = !show">放大缩小动画</button>
<transition name="bounce">
<p v-show="show" >梦学谷——陪你学习,伴你成长</p>
</transition>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
new Vue({
el: '#demo',
data: {
show: true
}
})
</script>
</body>
</html>