Vue第12话 过渡与动画

逆流者 2020年01月04日 55次浏览

1.vue 动画

  • 操作 css 的 trasition 或 animation
  • vue 会给目标元素添加/移除特定的 class
  • 过渡的相关类名

    xxx-enter-active: 指定显示的 transition

    xxx-leave-active: 指定隐藏的 transition

    xxx-enter/xxx-leave-to: 指定隐藏时的样式
    image.png

2.基本过渡动画的编码

  • 在目标元素外包裹
  • 定义 class 样式
    指定过渡样式: transition

指定隐藏时的样式: opacity/其它

3.编码

编码1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>11-过渡与动画</title>
    <style>
        .fade-enter-active, .fade-leave-active {
            transition: opacity .5s;
        }
        .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
            opacity: 0;
        }

        /* 可以设置不同的进入和离开动画 */
        /* 设置持续时间和动画函数 */
        .slide-fade-enter-active {
            transition: all .3s ease;
        }
        .slide-fade-leave-active {
            transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
        }
        .slide-fade-enter, .slide-fade-leave-to
            /* .slide-fade-leave-active for below version 2.1.8 */ {
            transform: translateX(10px);
            opacity: 0;
        }
    </style>
</head>
<body>
<div id="app-11-1">
    <button v-on:click="show = !show">
        Toggle
    </button>
    <transition name="fade">
        <p v-if="show">hello</p>
    </transition>
</div>
<div id="app-11-2">
    <button @click="show = !show">
        Toggle render
    </button>
    <transition name="slide-fade">
        <p v-if="show">hello</p>
    </transition>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
    new Vue({
        el: '#app-11-1',
        data: {
            show: true
        }
    });
    new Vue({
        el: '#app-11-2',
        data: {
            show: true
        }
    })
</script>
</body>
</html>

编码2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>11-过渡与动画-2</title>
    <style>
        .bounce-enter-active {
            animation: bounce-in .5s;
        }
        .bounce-leave-active {
            animation: bounce-in .5s reverse;
        }
        @keyframes bounce-in {
            0% {
                transform: scale(0);
            }
            50% {
                transform: scale(1.5);
            }
            100% {
                transform: scale(1);
            }
        }
    </style>
</head>
<body>
<div id="app-11-2">
    <button @click="show = !show">Toggle show</button><br>
    <transition name="bounce">
        <p v-if="show" style="display: inline-block">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris facilisis enim libero, at lacinia diam fermentum id. Pellentesque habitant morbi tristique senectus et netus.</p>
    </transition>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
    new Vue({
        el: '#app-11-2',
        data: {
            show: true
        }
    })
</script>
</body>
</html>