在Vue中使用 animate.css 庫

要在class的樣式中加上transform-origin: left center,不然可能會出問題:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
    <style type="text/css">
        @keyframes bounce-in {
            0% {
                transform: scale(0);
            }
            50% {
                transform: scale(15);
            }
            100% {
                transform: scale(1);
            }
        }
        .fade-enter-active {
            transform-origin: left center;
            animation: bounce-in 1s;
        }
        .fade-leave-active {
            transform-origin: left center;
            animation: bounce-in 1s reverse;
        }
    </style>
</head>
<body>
    <div id="root">
        //name隨便取名:
        <transition name="fade">
            <div v-show="show">hello</div>
        </transition>
        <button @click="handleClick">切換</button>
    </div>
    <script type="text/javascript">
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handleClick: function() {
                    this.show = !this.show
                }
            }
        });
    </script>
</body>
</html>

當然,還可以自定義style中的class名:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
    <style type="text/css">
        @keyframes bounce-in {
            0% {
                transform: scale(0);
            }
            50% {
                transform: scale(15);
            }
            100% {
                transform: scale(1);
            }
        }
        /*.fade-enter-active {
            transform-origin: left center;
            animation: bounce-in 1s;
        }
        .fade-leave-active {
            transform-origin: left center;
            animation: bounce-in 1s reverse;
        }*/
        .haha-active {
            transform-origin: left center;
            animation: bounce-in 1s;
        }
        .haha-leave {
            transform-origin: left center;
            animation: bounce-in 1s reverse;
        }

    </style>
</head>
<body>
    <div id="root">
        //name隨便取名:
        <!-- <transition name="fade">
            <div v-show="show">hello</div>
        </transition> -->
        //自定義class名字:
        <transition name="fade" enter-active-class="haha-active" leave-active-class="haha-leave">
            <div v-show="show">hello</div>
        </transition>
        <button @click="handleClick">切換</button>
    </div>
    <script type="text/javascript">
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handleClick: function() {
                    this.show = !this.show
                }
            }
        });
    </script>
</body>
</html>

自己命名class名有個好處,可以用別人的動畫。css在這個網址下載:https://daneden.github.io/animate.css/

<!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> -->
</head>
<body>
    <div id="root">
        //name隨便取名:
        <!-- <transition name="fade">
            <div v-show="show">hello</div>
        </transition> -->
        //自定義class名字:
        <transition name="fade" enter-active-class="animated rubberBand" leave-active-class="animated hinge">
            <div v-show="show">hello</div>
        </transition>
        <button @click="handleClick">切換</button>
    </div>
    <script type="text/javascript">
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handleClick: function() {
                    this.show = !this.show
                }
            }
        });
    </script>
</body>
</html>


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