Vue 动画 —— 滑动切换动画 / 滑动翻页过渡动画——从顶部到底部、从底部到顶部、从左侧到右侧、从右侧到左侧

简介: Vue 动画 —— 滑动切换动画 / 滑动翻页过渡动画——从顶部到底部、从底部到顶部、从左侧到右侧、从右侧到左侧

要点:

  1. 动画容器为相对定位,动画元素为绝对定位
  2. 每个动画元素都需单独用 <transition></transition> 包裹来添加过渡动画效果

完整演示范例代码

<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>
目录
相关文章
|
1天前
|
JavaScript
vue 组件封装 | s-fullpage 全屏滚动 (内含绑定鼠标滑轮滚动事件、避免鼠标滑轮连续滚动、滑动过渡动画等实用技巧)
vue 组件封装 | s-fullpage 全屏滚动 (内含绑定鼠标滑轮滚动事件、避免鼠标滑轮连续滚动、滑动过渡动画等实用技巧)
13 4
|
1天前
|
JavaScript
vue拖拽 —— vuedraggable 表格拖拽行
vue拖拽 —— vuedraggable 表格拖拽行
4 1
|
1天前
|
JavaScript
vue 动态绑定方法
vue 动态绑定方法
6 0
|
1天前
|
JavaScript
vue 手动/局部刷新组件
vue 手动/局部刷新组件
5 0
|
1天前
|
JavaScript 搜索推荐
vue【详解】props —— 子组件接收父组件传入的参数
vue【详解】props —— 子组件接收父组件传入的参数
6 0
|
前端开发 JavaScript
初识 Vue(24)---(Vue 中同时使用过渡和动画)
Vue 中同时使用过渡和动画 在上篇博客 《Vue 中使用 animate.css 库》基础上开始这篇博客 在上篇博客中,完成了 引入 animate.
1220 0
|
前端开发 内存技术
Vue_同时使用过渡和动画
在上一节我们用animate动画库,在刷新页面时没有动画 如何解决第一次就显示动画内容呢? 在transform 上加上appear 和appear-active-class <transition name='fade' appear enter-active-class='animate.
1541 0
|
1天前
|
JavaScript
vue组件封装 | 数字输入框(限制只能输入数字的input,可以指定小数点位数,最大值、最小值)
vue组件封装 | 数字输入框(限制只能输入数字的input,可以指定小数点位数,最大值、最小值)
15 7
|
2天前
|
JavaScript
vue 组件封装 | 图片放大镜(同天猫、淘宝、京东等商品图片放大浏览效果)
vue 组件封装 | 图片放大镜(同天猫、淘宝、京东等商品图片放大浏览效果)
12 6
|
2天前
|
JavaScript
vue报错 | Duplicate keys detected: ‘0’. This may cause an update error.
vue报错 | Duplicate keys detected: ‘0’. This may cause an update error.
9 3