多个元素或组件的过渡

多个元素过渡

 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
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Vue中多个元素或组件的过渡</title>
    <script src="./vue.js"></script>
    <style>
        .v-enter, .v-leave-to {
            opacity: 0;
        }
        .v-enter-active, .v-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <transition mode="out-in">
            <div v-if="show" key="hello">hello world</div>
            <div v-else key="bye">Bye world</div>
        </transition>   
        <button @click="handleClick">切换</button>
    </div> 
    <script>
    new Vue({ 
        el: "#root", 
        data: {
            show: true
        },
        methods: {
            handleClick: function() {
                this.show = !this.show
            },
        }
    })
    </script>
</body>
</html>

多个组件过渡

 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
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Vue中多个元素或组件的过渡</title>
    <script src="./vue.js"></script>
    <style>
        .v-enter, .v-leave-to {
            opacity: 0;
        }
        .v-enter-active, .v-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <transition mode="out-in">
            <child v-if="show"></child>
            <child-one v-else></child-one>
        </transition>   
        <button @click="handleClick">切换</button>
    </div> 
    <script>
    Vue.component('child', {
        template: '<div>child</div>'
    })
    Vue.component('child-one', {
        template: '<div>child-one</div>'
    })
    new Vue({ 
        el: "#root", 
        data: {
            show: true
        },
        methods: {
            handleClick: function() {
                this.show = !this.show
            },
        }
    })
    </script>
</body>
</html>
 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
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Vue中多个元素或组件的过渡</title>
    <script src="./vue.js"></script>
    <style>
        .v-enter, .v-leave-to {
            opacity: 0;
        }
        .v-enter-active, .v-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <transition mode="out-in">
            <component :is="type"></component>
        </transition>   
        <button @click="handleClick">切换</button>
    </div> 
    <script>
    Vue.component('child', {
        template: '<div>child</div>'
    })
    Vue.component('child-one', {
        template: '<div>child-one</div>'
    })
    new Vue({ 
        el: "#root", 
        data: {
            type: 'child'
        },
        methods: {
            handleClick: function() {
                this.type = this.type === 'child' ? 'child-one' : 'child'
            },
        }
    })
    </script>
</body>
</html>