vue中的动画

作者: 石燕平_Leo | 来源:发表于2018-04-12 16:18 被阅读0次

vue中的动画主要依靠transition这个控件,关于transition这个api可以上官网查看vue中的transition

vue中使用css动画

<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Vue中CSS动画原理</title>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <style>
        .fade-enter,
        .fade-leave-to {
            opacity: 0;
        }

        .fade-enter-active,
        .fade-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>

<body>
    <div id="container">
        <transition name="fade">
            <component :is="tabName"></component>
        </transition>

        <button @click="switchNav">switch</button>
    </div>
</body>
<script src="https://cdn.bootcss.com/vue/2.5.0/vue.js"></script>
<script>
    Vue.component('child-one', {
        template: `<div v-once>child-one</div>`
    });

    Vue.component('child-two', {
        template: `<div v-once>child-two</div>`
    });

    var vm = new Vue({
        el: "#container",
        data: function () {
            return {
                tabName: 'child-one'
            }
        },
        methods: {
            switchNav: function () {
                this.tabName = this.tabName == 'child-one' ? 'child-two' : 'child-one'
            }
        }
    });
</script>

</html>

vue中 animate.css 动画

<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title>在Vue中使用 animate.css 库</title>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" href="https://cdn.bootcss.com/animate.css/3.5.2/animate.min.css">
</head>

<body>
    <div id="container">
        <transition enter-active-class="animated fadeIn"
                    leave-active-class="animated shake">
            <div v-show="show">animation</div>
        </transition>

        <button @click="switchNav">toggle</button>
    </div>
</body>
<script src="https://cdn.bootcss.com/vue/2.5.0/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#container",
        data: function () {
            return {
                show: true
            }
        },
        methods: {
            switchNav: function () {
                this.show = !this.show;
            }
        }
    });
</script>

</html>

在vue中同时使用过渡和动画

<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title>在Vue中同时使用过渡和动画</title>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" href="https://cdn.bootcss.com/animate.css/3.5.2/animate.min.css">
    <style>
        .fade-enter,
        .fade-leave-to {
            opacity: 0;
        }
        .fade-enter-active,
        .fade-leave-active {
            transition: opacity 2s;
        }
    </style>
</head>

<body>
    <div id="container">
        <transition name="fade"
                    appear
                    :duration="{enter: 1000, leave: 3000}"
                    enter-active-class="animated jello fade-enter-active"
                    leave-active-class="animated tada fade-leave-active"
                    appear-active-class="animated jello fade-enter-active">
            <div v-show="show">animation</div>
        </transition>

        <button @click="switchNav">toggle</button>
    </div>
</body>
<script src="https://cdn.bootcss.com/vue/2.5.0/vue.js"></script>
<script>
    var vm = new Vue({
        el: "#container",
        data: function () {
            return {
                show: true
            }
        },
        methods: {
            switchNav: function () {
                this.show = !this.show;
            }
        }
    });
</script>

</html>

Vue中的 Js 动画与 Velocity.js 的结合

<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Vue中的 Js 动画与 Velocity.js 的结合</title>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" href="https://cdn.bootcss.com/animate.css/3.5.2/animate.min.css">
</head>

<body>
    <div id="container">
        <transition @before-enter="handleBeforeEnter"
                    @enter="handleEnter"
                    @after-enter="handleAfterEnter">
            <div v-show="show">animation</div>
        </transition>

        <button @click="switchNav">toggle</button>
    </div>
</body>
<script src="https://cdn.bootcss.com/vue/2.5.0/vue.js"></script>
<script src="https://cdn.bootcss.com/velocity/2.0.2/velocity.js"></script>
<script>
    var vm = new Vue({
        el: "#container",
        data: function () {
            return {
                show: true
            }
        },
        methods: {
            switchNav: function () {
                this.show = !this.show;
            },
            handleBeforeEnter: function(el) {
                el.style.opacity = 0;
            },
            handleEnter: function(el, done) {
                Velocity(el, {opacity: 1}, {duration: 1000, complete: done});
            },
            handleAfterEnter: function(el) {
                console.log('动画结束');
            }
        }
    });
</script>

</html>

Vue中多个元组件的过渡

<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Vue中多个元组件的过渡</title>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <style>
        .v-enter,
        .v-leave-to {
            opacity: 0;
        }

        .v-enter-active,
        .v-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>

<body>
    <div id="container">
        <transition mode="out-in">
            <component :is="type"></component>
        </transition>

        <button @click="switchNav">toggle</button>
    </div>
</body>
<script src="https://cdn.bootcss.com/vue/2.5.0/vue.js"></script>
<script>

    Vue.component('child-one', {
        template: '<div>child-one</div>'
    })

    Vue.component('child-two', {
        template: '<div>child-two</div>'
    })

    var vm = new Vue({
        el: "#container",
        data: function () {
            return {
                type: 'child-one'
            }
        },
        methods: {
            switchNav: function () {
                this.type = this.type == 'child-one' ? 'child-two' : 'child-one';
            }
        }
    });
</script>

</html>

封装vue中的动画组件

<html>

<head>
    <meta charset="UTF-8">
    <title>封装vue中的动画组件</title>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
</head>

<body>
    <div id="container">
        <fade :show="show">
            <div>hello transition</div>
        </fade>
        <button @click="handleClick">toggle</button>
    </div>
</body>
<script src="https://cdn.bootcss.com/vue/2.5.0/vue.js"></script>
<script>
    Vue.component('fade', {
        props: ['show'],
        template: `
            <transition @before-enter="handleBeforeEnter"
                        @enter="handleEnter">
                <slot v-if="show"></slot>
            </transition>
        `,
        methods: {
            handleBeforeEnter: function(el) {
                el.style.opacity = 0;
            },
            handleEnter: function(el, done) {
                setTimeout(() => {
                    el.style.opacity = 1;
                    done();
                }, 1000);
            }
        }
    })
    var vm = new Vue({
        el: "#container",
        data: function () {
            return {
                show: true
            }
        },
        methods: {
            handleClick: function () {
                this.show = !this.show;
            }
        }
    });
</script>

</html>

相关文章

  • vue的动画特效

    一、Vue中的CSS动画原理transition标签 二、Vue中的JS动画与velocity.js 三、Vue中...

  • Vue动画

    Vue动画 Vue动画 CSS transition 在进入/离开的过渡中,会有 6 个 class 切换。 对于...

  • Vue中的CSS动画原理(一)

    这里以Vue中的transition过渡动画为例介绍Vue中的CSS动画原理。效果如图1所示,通过toggle...

  • vue零基础开发025——Vue中Js动画与velocity.j

    【Vue中Js动画】 【velocity.js】

  • vue中动画

    1. 下载并且引入Animate.css 2. 在html中添加 class="animated" transit...

  • vue中的动画

    vue中的动画主要依靠transition这个控件,关于transition这个api可以上官网查看vue中的tr...

  • Vue中的动画

    Vue动画 Vue动画API 定义不同的名称动画 第三方动画库Animate.css和Vue的结合使用 enter...

  • Vue中的动画

    过渡的类名 在进入/离开的过渡中,会有 6 个 class 切换。 1. v-enter:定义进入过渡的开始状态...

  • vue中的动画

    v-if操作的是DOMv-show操作的是样式如果频繁切换dom使用v-show,当数据一开始就确定下来使用v-i...

  • Vue中的动画

    基础CSS过渡和动画 动画:keyframes过渡:transition 列表动画 状态动画 组件和元素切换 1....

网友评论

    本文标题:vue中的动画

    本文链接:https://www.haomeiwen.com/subject/decfkftx.html