动画——Vue中的动画封装

封装动画让代码可复用

如下是一个简单的点击渐变、渐隐:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="./animate.css">
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
    <style type="text/css">
        /*因为没有给它命名,所以用默认v开头的class名*/
        .v-enter, .v-leave-to {
            opacity: 0;
        }
        .v-enter-active, .v-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        //性能考虑,尽量不用index作为key值 <br>
        <transition>
            <div v-show="show">hello</div>
        </transition>
        <button @click="handBtnClick">toggle</button>
    </div>
    <script type="text/javascript">
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handBtnClick: function() {
                    this.show = !this.show
                }
            }
        });
    </script>
</body>
</html>

封装后:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="./animate.css">
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
    <style type="text/css">
        /*因为没有给它命名,所以用默认v开头的class名*/
        .v-enter, .v-leave-to {
            opacity: 0;
        }
        .v-enter-active, .v-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <fade :show="show">
            <div>hello world</div>
        </fade>
        <fade :show="show">
            <h1>hello world</h1>
        </fade>
        <button @click="handBtnClick">toggle</button>
    </div>
    <script type="text/javascript">
        Vue.component("fade", {
            props: ["show"],
            template: `
                <transition>
                    <slot v-if="show"></slot>
                </transition>
            `
        });
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handBtnClick: function() {
                    this.show = !this.show
                }
            }
        });
    </script>
</body>
</html>

其实css也可以封装:可以不用css动画,而用js动画:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="./animate.css">
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
    <style type="text/css">
        /*因为没有给它命名,所以用默认v开头的class名*/
        .v-enter, .v-leave-to {
            opacity: 0;
        }
        .v-enter-active, .v-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <fade :show="show">
            <div>hello world</div>
        </fade>
        <fade :show="show">
            <h1>hello world</h1>
        </fade>
        <button @click="handBtnClick">toggle</button>
    </div>
    <script type="text/javascript">
        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() //再次注意:要记得done()
                    }, 1000)
                }
            }
        });
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handBtnClick: function() {
                    this.show = !this.show
                }
            }
        });
    </script>
</body>
</html>

(这种封装可以完整的将css、js、html都封装在一个组件中。推荐

猜你喜欢

转载自blog.51cto.com/5660061/2420879