vue2.0的动画和1.0的动画

相对于vue1.0来说,vue2.0的动画变化还是挺大的,在1.0中,直接在元素中加 transition ,后面跟上名字。而在vue2.0中,需要把设置动画的元素、路由放在<transition name="fade"></transition>标签中,name就是动画名称。

在1.0时,css需要设置(动画名称以fade为例).fade-transition .fade-enter .fade-leave

vue1.0动画

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>vue1.0动画</title>
    <script type="text/javascript" src="js/vue.js"></script>
    <style>
        .oDiv {
            width: 200px;
            height: 200px;
            border: 3px dashed red;
            background: coral;
        }

        .fade-transition {
            transition: 2s all ease;
        }

        .fade-enter {
            opacity: 0;
        }

        .fade-leave {
            opacity: 0;
            transform: translate(200px);
        }
    </style>
</head>
<body>
<div id="box">
    <input type="button" value="button" @click="toggle()"/>
    <div class="oDiv" v-show="Dist" transition="fade">{{Dist}}</div>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#box',
        data: {
            Dist: false
        },
        methods: {
            toggle: function () {
                this.Dist = !this.Dist;
            }
        }
    })
</script>
</html>

在2.0时,css设置大改,.fade-enter{}元素初始状态 .fade-enter-active{}元素最终状态 .fade-leave-active{} 元素离开的最终状态

vue2.0动画

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>vue2.0动画</title>
    <script type="text/javascript" src="js/vue.js"></script>
    <style>
        p{
            width: 300px;
            height: 300px;
            background-color: yellow;
        }
        .donghua-enter-active,
        .donghua-leave-active{
             transition: 5s all ease;
        }
        .donghua-enter-active{
            opacity: 1;
            width: 300px;
            height: 300px;
        }
        .donghua-leave-active{
            opacity: 0;
            width: 100px;
            height: 100px;
        }
        .donghua-enter,.donghua-leave{
            opacity: 0;
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
<div id="box">
    <input type="button" value="点击显示隐藏" @click="show=!show">
    <transition name="donghua">
        <p v-show="show"></p>
    </transition>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#box',
        data: {
            show: false
        }
    })
</script>
</html>

在2.0中,依然可以与animate.css结合起来一起写动画

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <link rel="stylesheet" type="text/css" href="css/animate.min.css"/>
    <title>配合animate.css做动画</title>
    <script type="text/javascript" src="js/vue.js"></script>
    <style>
        p{
            width: 300px;
            height: 300px;
            background-color: yellow;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div id="box">
    <input type="button" value="点击显示隐藏" @click="show=!show">
    <transition enter-active-class="zoomOutDown" leave-active-class="zoomOutUp">
        <p v-show="show" class="animated"></p>
    </transition>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#box',
        data: {
            show: false
        }
    })
</script>
</html>

在2.0中,关于动画还为我们提供了一些事件

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>动画事件</title>
    <script type="text/javascript" src="js/vue.js"></script>
    <style>
        p{
            width: 300px;
            height: 300px;
            background-color: yellow;
        }
        .donghua-enter-active,
        .donghua-leave-active{
            transition: 5s all ease;
        }
        .donghua-enter-active{
            opacity: 1;
            width: 300px;
            height: 300px;
        }
        .donghua-leave-active{
            opacity: 0;
            width: 100px;
            height: 100px;
        }
        .donghua-enter,.donghua-leave{
            opacity: 0;
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
<div id="box">
    <input type="button" value="点击显示隐藏" @click="show=!show">
    <transition name="donghua"
                @before-enter="beforeEnter"
                @enter="Enter"
                @after-enter="afterEnter"
                @before-leave="beforeLeave"
                @leave="Leave"
                @after-leave="afterLeave"
    >
        <p v-show="show"></p>
    </transition>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#box',
        data: {
            show: false
        },
        methods: {
            beforeEnter() {
                console.log("<!-- 进入动画开始之前 -->");
            },
            Enter() {
                console.log("<!-- 正式进入动画 -->");
            },
            afterEnter(el) {
                console.log("<!-- 动画结束 -->");
            },
            beforeLeave(el) {
                console.log("<!-- 离开动画开始之前 -->");
                el.style.background='blue';    //改变颜色
                el.innerHTML="123";
            },
            Leave(){
                console.log("<!-- 正在离开动画-->");
            },
            afterLeave(){
                console.log("<!-- 离开动画结束 -->");
            },
        }
    })
</script>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容