Vue中多个元素或组件的过渡

发布时间:2020-06-26 05:25:27 作者:梁十八
来源:网络 阅读:307
<!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>
        .myfade-enter, .v-leave-to {
            opacity: 0;
        }
        .v-enter-active, .v-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <transition name="myfade">
            <div v-if="show">hello</div>
            <div v-else>bye</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并不会实现渐变效果,因为vue默认是会尽量复用dom,想要vue不复用dom,要给其加上不同的key值)

加上不同key值后,渐变效果有了:

<!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>
        .myfade-enter, .myfade-leave-to {
            opacity: 0;
        }
        .myfade-enter-active, .myfade-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <transition name="myfade">
            <div v-if="show" key="hello">hello</div>
            <div v-else key="bye">bye</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>

如果想设置多个属性之间的切换效果,可以用mode(mode="in-out":先显示要显示的再隐藏要隐藏的。mode="out-in":和前面的相反):

<!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>
        .myfade-enter, .myfade-leave-to {
            opacity: 0;
        }
        .myfade-enter-active, .myfade-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <transition name="myfade" mode="out-in">
            <div v-if="show" key="hello">hello</div>
            <div v-else key="bye">bye</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>

组件动画也是可以的(不需要上面的不同值的key):

<!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>
        .myfade-enter, .myfade-leave-to {
            opacity: 0;
        }
        .myfade-enter-active, .myfade-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <transition name="myfade" mode="out-in">
            <child1 v-if="show">hello</child1>
            <child2 v-else>bye</child2>
        </transition>
        <button @click="handleClick">切换</button>
    </div>
    <script type="text/javascript">
        Vue.component("child1", {
            template: "<div>child1</div>"
        });
        Vue.component("child2", {
            template: "<div>child2</div>"
        });
        var vm = new Vue({
            el: "#root",
            data: {
                show: true
            },
            methods: {
                handleClick: 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>
        .myfade-enter, .myfade-leave-to {
            opacity: 0;
        }
        .myfade-enter-active, .myfade-leave-active {
            transition: opacity 1s;
        }
    </style>
</head>
<body>
    <div id="root">
        <transition name="myfade" mode="out-in">
            //通过动态组件的方式实现:
            <component :is="type"></component>
        </transition>
        <button @click="handleClick">切换</button>
    </div>
    <script type="text/javascript">
        Vue.component("child1", {
            template: "<div>child1</div>"
        });
        Vue.component("child2", {
            template: "<div>child2</div>"
        });
        var vm = new Vue({
            el: "#root",
            data: {
                type: "child1"
            },
            methods: {
                handleClick: function() {
                    this.type = this.type == "child1" ? "child2" : "child1"
                }
            }
        });
    </script>
</body>
</html>


推荐阅读:
  1. Vue中的列表过渡
  2. vue如何访问元素和组件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue中多个元素或组件的过渡 ue 多个元素

上一篇:IPv6与IPv4互访

下一篇:Visual Studio Scrum

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》