動畫——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都封裝在一個組件中。推薦

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章