vue2.0之过渡动画,分别用钩子函数,animated,原生css实现(前端网备份) 结合animated实现vue过渡 css原生实现的过渡和动画
钩子函数实现过渡,要求每次点击按钮只能从右边100px往左边滑动,
.show{ transition: all 0.4s ease } js <div > <button @click="show = !show">Toggle render</button> <transition @before-enter="beforeEnter" v-on:enter="enter" v-on:after-enter="afterEnter"> <p v-if="show" class="show">hello</p> </transition> </div> export default { name: "HelloWorld", data() { return { show: false, }; }, props: { }, created: function() { }, methods: { beforeEnter(el) { el.style.transform = "translate(100px,0)"; console.log(el); }, enter(el, done) { //动画初始化 el.offsetWidth; el.style.transform = "translate(0px,0)"; //动画结束 done(); console.log(el); console.log(done); }, afterEnter(el) { this.show = !this.show; }, } };
<transition enter-active-class="animated fadeInRight " leave-active-class="animated bounceOutRight" > <p v-if="show">hello</p> </transition>
<link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
其实只需要把enter-active-class和leave-active-class写成一样的,也可以达到上面钩子函数每次点击按钮都是从右边划到左边的动画效果
<transition enter-active-class="animated fadeInRight " leave-active-class="animated fadeInRight" > <p v-if="show">hello</p> </transition>
css原生实现的过渡和动画
.show{ transition: all 0.4s ease}
js<div ; //动画结束 done(); console.log(el); console.log(done); }, afterEnter(el) { this.show = !this.show; }, }};