Vue2.5开发去哪儿网App 第五章笔记 下

1. 多个元素或组件的过渡

多个元素的过渡:

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

多个组件的过渡:

    <transition>
        <child-one v-if="show"></child-one>
        <child-two v-else></child-two>
    </transition>

通过component标签

    <transition mode="out-in">
        <component :is="type">

        </component>
    </transition>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多个元素或组件的过渡</title>
    <script src="../../vue.js"></script>
    <style>
        .v-enter,.v-leace-to{
            opacity: 0;
        }
        .v-enter-active,.v-leave-active{
            transition: opacity 1s;
        }
    </style>
</head>
<body>
<div id="app">
    <!--<transition mode="out-in">-->
        <!--<div v-if="show" key="hello">hello world</div>-->
        <!--<div v-else key="bye">Bye world</div>-->
    <!--</transition>-->

    <!--<transition>-->
        <!--<child-one v-if="show"></child-one>-->
        <!--<child-two v-else></child-two>-->
    <!--</transition>-->
    <!--<button @click="handleCLick">click me</button>-->
    <!--通过动态组件的方式-->
    <transition mode="out-in">
        <component :is="type">

        </component>
    </transition>
    <button @click="handlechildCLick">click me</button>
</div>
<script>
    Vue.component('child-one',{
        template:'<div>child one</div>'
    })

    Vue.component('child-two',{
        template:'<div>child two</div>'
    })

    var vm = new Vue({
        el:"#app",
        data:{
            show:true,
            type:'child-one'
        },
        methods: {
            handleCLick: function () {
                this.show = !this.show
            },
            handlechildCLick:function () {
                this.type = this.type==='child-one'?'child-two':'child-one'
            }
        }
    })
</script>
</body>
</html>
View Code
2.列表过渡

    <transition-group>
        <div v-for="item in list" :key="item.id">
            {{item.title}}
        </div>
    </transition-group>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>列表过渡</title>
    <script src="../../vue.js"></script>
    <style>
        .v-enter,.v-leace-to{
            opacity: 0;
        }
        .v-enter-active,.v-leave-active{
            transition: opacity 1s;
        }
    </style>
</head>
<body>
<div id="app">
    <transition-group>
        <div v-for="item in list" :key="item.id">
            {{item.title}}
        </div>
    </transition-group>
    <button @click="handleCLick">click me</button>
</div>
<script>
    var count = 0;
    var vm = new Vue({
        el:"#app",
        data:{
            list:[]
        },
        methods:{
            handleCLick:function () {
                this.list.push({
                    id:count++,
                    title:'hello world'
                })
            }
        }
    })
</script>
</body>
</html>
View Code
3.动画封装

        template:`<transition @before-enter="handleBeforeEnter" @enter="handleEneter">
                    <slot v-if="show"></slot>
                    </transition>`,
 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动画封装</title>
    <script src="../../vue.js"></script>
</head>
<body>
<div id="app">
    <fade :show="show">
        <div>Hello world</div>
    </fade>

    <fade :show="show">
        <h1>Hello world</h1>
    </fade>
    <button @click="handleCLick">click me</button>
</div>
<script>
    Vue.component('fade',{
        props:['show'],
        template:`<transition @before-enter="handleBeforeEnter" @enter="handleEneter">
                    <slot v-if="show"></slot>
                    </transition>`,
        methods:{
            handleBeforeEnter:function (el) {
                    el.style.color = 'red'
            },
            handleEneter:function (el,done) {
                setTimeout(()=>{
                    el.style.color = 'green'
                    done()
                },2000)
            }
        }
    });
    var count = 0;
    var vm = new Vue({
        el:"#app",
        data:{
            show:true
        },
        methods:{
            handleCLick:function () {
                this.show = !this.show;
            }
        }
    })
</script>
</body>
</html>
View Code
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值