美文网首页
vue零基础开发028——Vue中的动画封装

vue零基础开发028——Vue中的动画封装

作者: 文朝明 | 来源:发表于2019-12-11 17:37 被阅读0次

【动画封装(不带效果)】

<html>
<head>
    <meta charset="utf-8" />
    <title>Vue中的动画封装</title>
    <script src="./vue.js"></script>
    <style>
        .v-enter, .v-leave {
            opacity: 0
        }

        .v-enter-active, .v-leave-active {
            transition: opacity 1s
        }
    </style>
</head>
<body>
    <div id="root">
        <fade :show="show">
            <div>hellow world</div>
        </fade>
        <fade :show="show">
            <h1>hellow world</h1>
        </fade>
        <button @click="handleBtnClick">toggle</button>
    </div>
    <script>
        //封装
        Vue.component('fade', {
            props: ['show'],
            template: '<transition><slot v-if="show"></slot></transition> '
        })
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handleBtnClick: function () {
                    this.show = !this.show
                },
            }
        })

    </script>
</body>
</html>

动画封装(效果单独 )

【动画封装(效果一起)】

<html>
<head>
    <meta charset="utf-8" />
    <title>Vue中的动画封装</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="root">
        <fade :show="show">
            <div>hellow world</div>
        </fade>
        <fade :show="show">
            <h1>hellow world</h1>
        </fade>
        <button @click="handleBtnClick">toggle</button>
    </div>
    <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.color = 'red'
                },
                handleEnter: function (el, done) {
                    setTimeout(() => {
                        el.style.color = 'green'
                        done()
                    }, 2000)
                }
            }
        })
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handleBtnClick: function () {
                    this.show = !this.show
                },
            }
        })

    </script>
</body>
</html>

动画封装(效果一起)
动画封装(效果一起)

相关文章

网友评论

      本文标题:vue零基础开发028——Vue中的动画封装

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