vue动画

vue动画

vue动画需要用transition将所需要过渡的元素包裹起来

//引入vue.js
<script src="./vue-2.4.0.js"></script> 
//css代码:
       #app {
            width: 600px;
            margin: auto;
        }

        .v-enter {
            transform: translateX(500px);
        }

        .v-enter-to {
            transform: translateX(0);
        }

        .v-enter-active {
            transition: all 3s;
        }

        .v-leave {
            transform: translateX(0);
        }

        .v-leave-to {
            transform: translateX(-500px);
        }

        .v-leave-active {
            transition: all 3s;
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: cornflowerblue;
        }

        .tra-enter {
            transform: translateX(-500px);
        }

        .tra-enter-to,
        .tra-leave {
            transform: translateX(0);
        }

        .tra-enter-active,
        .tra-leave-active {
            transition: all 3s;
        }
        .tra-leave-to {
            transform: translateX(500px);
        }
//html代码:
    <div id='app'>
        <button @click="change">change</button>
        <button @click="flag=!flag">changeFlag</button>
        <!-- vue.动画需要用transition将所需要的过渡元素包裹起来 -->
        <transition>
            <div v-show="flag" class="box">{{msg}}</div>
        </transition>
        <div v-show="flag">{{msg}}</div>
        <transition name="tra">
            <div v-show="flag" class="box">{{msg}}</div>
        </transition>
        })

第三方动画

第三方动画需要引入第三方组件

//引入vue.js
<script src="./vue-2.4.0.js"></script> 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
//css代码:
     
          #app {
            width: 500px;
            margin: 200px auto;
        }
//html代码:
    <div id='app'>
         <button @click="show = !show"> render</button>
        <transition name="custom-classes-transition" enter-active-class="animate__animated animate__bounceOutUp"
            leave-active-class="animate__animated animate__bounceOutUp">
            <p v-if="show">hello</p>
        </transition>
    </div>
//vue代码:
     const vm = new Vue({
            el: '#app',
            data: {
                show: true
            },
            methods: {
            }
        })

动画钩子函数

//引入vue.js
<script src="./vue-2.4.0.js"></script> 
//css代码:
         #app {
            width: 600px;
            margin: auto;
        }

        .show {
            transition: all 0.4s ease;
        }
//html代码:
    <div id='app'>
        <input type="button" value="切换动画" @click="isshow = !isshow">
        <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
            <div v-if="isshow" class="show">OK</div>
            <!-- <div>123</div> -->
        </transition>
    </div>
//vue代码:
     const vm = new Vue({
            el: '#app',
            data: {
                isshow: true
            },
            methods: {
                beforeEnter(el) { // 动画进入之前的回调
                    el.style.transform = 'translateX(500px)';
                },
                enter(el, done) { // 动画进入完成时候的回调
                    console.log(done);
                    // 必须添加
                    el.offsetWidth;
                    el.style.transform = 'translateX(0px)';
                    done();
                },
                afterEnter(el) { // 动画进入完成之后的回调
                    this.isshow = !this.isshow;
                }
            }
        })

transition-group

//引入vue.js
<script src="./vue-2.4.0.js"></script> 
//css代码:
         #app {
            width: 600px;
            margin: auto;
        }

        .v-enter {
            transform: translateX(500px);
        }

        .v-enter-to {
            transform: translateX(0);
        }

        .v-enter-active {
            transition: all 3s;
        }

        .v-leave {
            transform: translateX(0);
        }

        .v-leave-to {
            transform: translateX(-500px);
        }

        .v-leave-active {
            transition: all 3s;
        }
//html代码:
    <div id='app'>
        <button @click="flag = !flag">flag</button>
        <button @click="add">add</button>
        <transition-group>
            <div v-for="(item,index) in list" :key="index" v-show="flag">{{item}}</div>
        </transition-group>
    </div>
//vue代码:
    const vm = new Vue({
            el: '#app',
            data: {
                flag: true,
                list: [1, 2, 3, 4]
            },
            methods: {
                add() {
                    this.list.push(this.list[this.list.length - 1] + 1)
                }
            }
        })

transition-group v-move可以改变定位

//引入vue.js
<script src="./vue-2.4.0.js"></script> 
//css代码:
         #app {
            width: 600px;
            margin: auto;
        }

        .v-enter {
            transform: translateX(500px);
        }

        .v-enter-to {
            transform: translateX(0);
        }

        .v-enter-active {
            transition: all 3s;
        }

        .v-leave {
            transform: translateX(0);
        }

        .v-leave-to {
            transform: translateX(-500px);
        }

        .v-leave-active {
            transition: all 3s;
        }

         .v-move {
            transition: all 0.8s ease;
        }

        .v-leave-active {
            position: absolute;
        }

        .v-enter-active {
            position: absolute;
        }
//html代码:
    <div id='app'>
        <button @click="flag = !flag">flag</button>
        <button @click="add">add</button>
        <transition-group>
            <div v-for="(item,index) in list" :key="index" v-show="flag">{{item}}</div>
        </transition-group>
    </div>
//vue代码:
    const vm = new Vue({
            el: '#app',
            data: {
                flag: true,
                list: [1, 2, 3, 4]
            },
            methods: {
                add() {
                    this.list.push(this.list[this.list.length - 1] + 1)
                }
            }
        })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值