使用vue动画完成滑入滑出切换

<template>
    <div style="padding: 30px">
        <button @click="play('slide_topToBottom')">从顶部滑入、底部滑出</button>
        <button @click="play('slide_bottomToTop')">从底部滑入、顶部滑出</button>
        <button @click="play('slide_leftToRight')">从左侧滑入、右侧滑出</button>
        <button @click="play('slide_rightToLeft')">从右侧滑入、左侧滑出</button>
        <div class="view">
        <transition :name="transName">
            <div v-show="active === 1" class="block red">第一页</div>
        </transition>
        <transition :name="transName">
            <div v-show="active === 2" class="block green">第二页</div>
        </transition>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            transName: "",
            active: 1
        }
    },
    methods: {
        play(name) {
            this.transName = name
            if (this.active === 1) {
                this.active = 2
            } else {
                this.active = 1
            }
        }
    }
}
</script>
    <style scoped>
    .view {
    position: relative;
    height: 100px;
    width: 200px;
    background: gainsboro;
    overflow: hidden;
    }

    .block {
    position: absolute;
    height: 100px;
    width: 200px;
    text-align: center;
    color: white;
    line-height: 100px;
    }

    .red {
    background: red;
    }

    .green {
    background: green;
    }

    /*滑入——从顶部*/
    @keyframes slideIn_top {
    0% {
        top: -100%;
    }
    100% {
        top: 0;
    }
    }

    /*滑入——从底部*/
    @keyframes slideIn_bottom {
    0% {
        top: 100%;
    }
    100% {
        top: 0;
    }
    }

    /*滑入——从左侧*/
    @keyframes slideIn_left {
    0% {
        left: -100%;
    }
    100% {
        left: 0;
    }
    }

    /*滑入——从右侧*/
    @keyframes slideIn_right {
    0% {
        left: 100%;
    }
    100% {
        left: 0;
    }
    }

    /*滑出——从顶部*/
    @keyframes slideOut_top {
    0% {
        top: 0;
    }
    100% {
        top: -100%;
    }
    }

    /*滑出——从底部*/
    @keyframes slideOut_bottom {
    0% {
        top: 0;
    }
    100% {
        top: 100%;
    }
    }

    /*滑出——从左侧*/
    @keyframes slideOut_left {
    0% {
        left: 0;
    }
    100% {
        left: -100%;
    }
    }

    /*滑出——从右侧*/
    @keyframes slideOut_right {
    0% {
        left: 0;
    }
    100% {
        left: 100%;
    }
}

/*滑动(滑入)——从顶部滑入,从底部滑出*/
.slide_topToBottom-enter-active {
    animation: slideIn_top 1s;
}

/*滑动(滑出)——从顶部滑入,从底部滑出*/
.slide_topToBottom-leave-active {
    animation: slideOut_bottom 1s;
}

/*滑动(滑入)——从底部滑入,从顶部滑出*/
.slide_bottomToTop-enter-active {
    animation: slideIn_bottom 1s;
}

/*滑动(滑出)——从底部滑入,从顶部滑出*/
.slide_bottomToTop-leave-active {
    animation: slideOut_top 1s;
}

/*滑动(滑入)——从左侧滑入,从右侧滑出*/
.slide_leftToRight-enter-active {
    animation: slideIn_left 1s;
}

/*滑动(滑出)——从左侧滑入,从右侧滑出*/
.slide_leftToRight-leave-active {
    animation: slideOut_right 1s;
}

/*滑动(滑入)——从右侧滑入,从左侧滑出*/
.slide_rightToLeft-enter-active {
    animation: slideIn_right 1s;
}

/*滑动(滑出)——从右侧滑入,从左侧滑出*/
.slide_rightToLeft-leave-active {
    animation: slideOut_left 1s;
}
</style>

  • 11
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值