源码
- 显示隐藏过渡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue动画</title>
<style type="text/css">
.fade-enter,.fade-leave-active{
opacity: 0;
}
.fade-enter-active,.fade-leave-active{
transition: opacity .5s;
}
</style>
<script src="https://cdn.bootcss.com/vue/2.5.13/vue.js"></script>
</head>
<body>
<div id="demo">
<button v-on:click="show = !show">
Toggle
</button>
<transition name="fade">
<p v-if="show">Hello Vue Transition</p>
</transition>
</div>
<script>
new Vue({
el:'#demo',
data:{
show:true
}
});
</script>
</body>
</html>
- 效果图
- 显示隐藏动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue动画</title>
<style type="text/css">
/*.fade-enter,.fade-leave-active{
opacity: 0;
}
.fade-enter-active,.fade-leave-active{
transition: opacity .5s;
}*/
.test-leave-active{
animation: scale-out .5s;
}
.test-enter-active{
animation: scale-in .5s;
}
@keyframes scale-in{
0%{
transform: scale(0);
}
50%{
transform: scale(1.5);
}
100%{
transform: scale(1);
}
}
@keyframes scale-out{
0%{
transform: scale(1);
}
50%{
transform: scale(1.5);
}
100%{
transform: scale(0);
}
}
</style>
<script src="https://cdn.bootcss.com/vue/2.5.13/vue.js"></script>
</head>
<body>
<div id="demo">
<button v-on:click="show = !show">
Toggle
</button>
<!-- <transition name="fade">
<p v-if="show">Hello Vue Transition</p>
</transition> -->
<transition name="test">
<p v-if="show">Hello Vue Transition</p>
</transition>
</div>
<script>
new Vue({
el:'#demo',
data:{
show:true
}
});
</script>
</body>
</html>
- 效果图
- 引用 Animate.css 动画库实现更加复杂的动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue动画</title>
<style type="text/css">
/*.fade-enter,.fade-leave-active{
opacity: 0;
}
.fade-enter-active,.fade-leave-active{
transition: opacity .5s;
}*/
/*.test-leave-active{
animation: scale-out .5s;
}
.test-enter-active{
animation: scale-in .5s;
}
@keyframes scale-in{
0%{
transform: scale(0);
}
50%{
transform: scale(1.5);
}
100%{
transform: scale(1);
}
}
@keyframes scale-out{
0%{
transform: scale(1);
}
50%{
transform: scale(1.5);
}
100%{
transform: scale(0);
}
}*/
</style>
<link href="https://cdn.bootcss.com/animate.css/3.5.2/animate.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/vue/2.5.13/vue.js"></script>
</head>
<body>
<div id="demo">
<button v-on:click="show = !show">
Toggle
</button>
<!-- <transition name="fade">
<p v-if="show">Hello Vue Transition</p>
</transition> -->
<!-- <transition name="test">
<p v-if="show">Hello Vue Transition</p>
</transition> -->
<transition name="custom-classes-transition" leave-active-class="animated jello" enter-active-class="animated bounce">
<p v-if="show">Hello Vue Transition</p>
</transition>
</div>
<script>
new Vue({
el:'#demo',
data:{
show:true
}
});
</script>
</body>
</html>
- 效果图
- 引用 velocity.js 动画库实现更加复杂的动画
官网示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue动画</title>
<style type="text/css">
/*.fade-enter,.fade-leave-active{
opacity: 0;
}
.fade-enter-active,.fade-leave-active{
transition: opacity .5s;
}*/
/*.test-leave-active{
animation: scale-out .5s;
}
.test-enter-active{
animation: scale-in .5s;
}
@keyframes scale-in{
0%{
transform: scale(0);
}
50%{
transform: scale(1.5);
}
100%{
transform: scale(1);
}
}
@keyframes scale-out{
0%{
transform: scale(1);
}
50%{
transform: scale(1.5);
}
100%{
transform: scale(0);
}
}*/
</style>
<!-- <link href="https://cdn.bootcss.com/animate.css/3.5.2/animate.css" rel="stylesheet"> -->
<script src="https://cdn.bootcss.com/vue/2.5.13/vue.js"></script>
<script src="https://cdn.bootcss.com/velocity/1.5.1/velocity.js"></script>
</head>
<body>
<div id="demo">
<button v-on:click="show = !show">
Toggle
</button>
<!-- <transition name="fade">
<p v-if="show">Hello Vue Transition</p>
</transition> -->
<!-- <transition name="test">
<p v-if="show">Hello Vue Transition</p>
</transition> -->
<!-- <transition name="custom-classes-transition" leave-active-class="animated jello" enter-active-class="animated bounce">
<p v-if="show">Hello Vue Transition</p>
</transition> -->
<transition v-on:before-enter="beforeEnter" v-bind:css="false" v-on:enter="enter">
<p v-if="show">Hello Vue Transition</p>
</transition>
</div>
<script>
new Vue({
el:'#demo',
data:{
show:true
},
methods:{
beforeEnter:function(el) {
el.style.opacity = 0;
el.style.height = 0;
},
enter:function(el,done){
Velocity(el,{
opacity:1,
fontSize:'1.4rem',
translateX:'20px',
rotateZ:'50deg'
},{
duration: 500
})
}
}
});
</script>
</body>
</html>
- 效果图