Vue的Js動畫與Velocity.js的結合

@before-enter,在隱藏後,點擊讓它顯示的時候,顯示之前會觸發。@enter不同,是在動畫執行的過程中觸發。done觸發完成之後就是@after-enter。這些是通過js鉤子來實現,也就是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> -->
</head>
<body>
    <div id="root">
        //自定義class名字:
        <transition 
            name="fade" 
            @before-enter="handleBeforeEnter"
            @enter="handleEnter"
            @after-enter="handleAfterEnter"
        >
            <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
                },
                //el指的是動畫包裹的標籤
                handleBeforeEnter: function(el) {
                    //console.log("handleBeforeEnter")
                    //在標籤從隱藏點擊顯示的時候,變成了紅色
                    el.style.color = "red"
                },
                //接收兩個回調函數,el同上,done
                handleEnter: function(el, done) {
                    //之前顯示是紅色,兩秒後變成綠色
                    setTimeout(()=>{
                        el.style.color = "green"
                    }, 2000)
                    setTimeout(()=>{
                        //但4秒才告訴vue結束(也就是2秒後變綠,又4-2秒後變黑)
                        done() //這個done很重要,執行完之後要告訴vue,已經執行完
                    }, 4000)
                },
                handleAfterEnter: function(el) {
                    el.style.color = "black"
                }
            }
        });
    </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> -->
</head>
<body>
    <div id="root">
        //自定義class名字:
        <transition 
            name="fade" 
            @before-leave="handleBeforeLeave"
            @leave="handleLeave"
            @after-leave="handleAfterLeave"
        >
            <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
                },
                //el指的是動畫包裹的標籤
                handleBeforeLeave: function(el) {
                    //console.log("handleBeforeLeave")
                    //在標籤從隱藏點擊顯示的時候,變成了紅色
                    el.style.color = "red"
                },
                //接收兩個回調函數,el同上,done
                handleLeave: function(el, done) {
                    //之前顯示是紅色,兩秒後變成綠色
                    setTimeout(()=>{
                        el.style.color = "green"
                    }, 2000)
                    setTimeout(()=>{
                        //但4秒才告訴vue結束(也就是2秒後變綠,又4-2秒後變黑)
                        done() //這個done很重要,執行完之後要告訴vue,已經執行完
                    }, 4000)
                },
                handleAfterLeave: function(el) {
                    setTimeout(()=>{
                        el.style.color = "black"
                    }, 2000)
                }
            }
        });
    </script>
</body>
</html>

使用velocity。下載地址:http://velocityjs.org/

<!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="./velocity.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
    <div id="root">
        //自定義class名字:
        <transition 
            name="fade" 
            @before-enter="handleBeforeEnter"
            @enter="handleEnter"
            @after-enter="handleAfterEnter"
        >
            <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
                },
                //el指的是動畫包裹的標籤
                handleBeforeEnter: function(el) {
                    el.style.opacity = 0;
                },
                //接收兩個回調函數,el同上,done
                handleEnter: function(el, done) {
                    //注意,要加上complete: done告訴vue結束
                    Velocity(el, {opacity:1}, {duration: 1000, complete: done})
                },
                handleAfterEnter: function(el) {
                    alert("動畫結束")
                }
            }
        });
    </script>
</body>
</html>


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