动画封装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>动画封装</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="root">
        <fade :show="show">
            <div>hello world</div>
        </fade>
        <fade :show="show">
            <h1>hello 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)
            }
        }
    })
    new Vue({ 
        el: "#root", 
        data: {
            show: false
        },
        methods: {
            handleBtnClick: function() {
                this.show = !this.show
            },
        }
    })
    </script>
</body>
</html>