• 【Vue3】 第十六部分 transition


    Vue3】 第十六部分 transition



    16. transition

    16.1 transition的基本使用(name属性)

    在这里插入图片描述

    <script lang="ts" setup>
        import {ref} from 'vue'
        const flag = ref<boolean>(true)
    </script>
    
    <template>
        <div>
            <button @click="flag = !flag">切换</button>
            <transition name="fade">
                <h1 v-if="flag">大家好!</h1>
            </transition>
        </div>
    </template>
    
    <style scoped lang='less'>
        // 根据所取的名字,会提供6个类
        .fade-enter-from{
            // 进入时的过渡前
            opacity: 0;
        }
        .fade-enter-active{
            // 进入时的过渡中
            transition: all 1s ease;
        }
        .fade-enter-to{
            // 进入时的过渡结束
            opacity: 1;
        }
        .fade-leave-from{
            // 离开时的过渡前
            opacity: 1;
        }
        .fade-leave-active{
            // 离开时的过渡中
            transition: all 1s ease;
        }
        .fade-leave-to{
            // 离开时的过渡结束
            opacity: 0;
        }
    </style>
    
    • 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

    16.2 transition+animate.css使用

    在这里插入图片描述

    安装animate.css

    $ npm install animate.css --save
    $ yarn add animate.css
    
    • 1
    • 2
    <script lang="ts" setup>
        import {ref} from 'vue'
    		// 引入样式
        import 'animate.css';
        const flag = ref<boolean>(true)
    </script>
    
    <template>
        <div>
            <button @click="flag = !flag">切换</button>
            <!-- 直接使用这两个属性,因为开头和结尾都只有一帧而已 -->
            <transition enter-active-class="animate__animated animate__bounceInDown" leave-active-class="animate__animated animate__fadeOutDown">
                <h1 v-if="flag">大家好!</h1>
            </transition>
        </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    16.3 transition中Appear属性

    这个属性可以在页面加载完后立即执行对应的状态,也就是立即执行动画过渡效果

    <script lang="ts" setup>
        import {ref} from 'vue'
        import 'animate.css';
        const flag = ref<boolean>(true)
    </script>
    
    <template>
        <div>
            <!-- 页面加载完后立即执行-->
            <transition appear appear-active-class="animate__animated animate__bounceInDown">
                <h1 v-if="flag">大家好!</h1>
            </transition>
        </div>
    </template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    16.4 transitionGroup

    16.4.1 配合animate使用

    在这里插入图片描述

    用于同时渲染整个列表,例如:使用v-for的时候

    <script lang="ts" setup>
        import {ref} from 'vue'
        import "animate.css"
        const list = ref<number[]>([1,2,3])
        const add = () =>{
            list.value.push(list.value.length + 1)
        }
        const del = () =>{
            list.value.pop()
        }
    </script>
    
    <template>
        <div>
            <div class="wrapper">
                <transition-group enter-active-class="animate__animated animate__lightSpeedInRight" leave-active-class="animate__animated animate__rotateOutDownLeft">
                    <div class="item" v-for="item in list" :key="item">{{item}}</div>
                </transition-group>
            </div>
            <button @click="add">add</button>
            <button @click="del">del</button>
        </div>
    </template>
    
    <style scoped lang='less'>
        .wrapper{
            display: flex;
            .item{
                margin: 5px;
            }
        }
    </style>
    
    • 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

    16.4.2 平移过渡动画

    在这里插入图片描述

    $ npm i -g npm
    $ npm i --save lodash
    
    • 1
    • 2
    <script lang="ts" setup>
    import _ from 'lodash';
    import { ref } from 'vue';
    let list = ref(Array.apply(null, { length: 81 } as number[]).map((_, index) => {
        return {
            id: index,
            num: index % 9 + 1
        }
    }))
    const handleRandom = () =>{
        list.value = _.shuffle(list.value)
    }
    </script>
    
    <template>
        <div>
            <button @click="handleRandom">随机排序</button>
            <div class="wrapper">
                <!-- move-class:平移变化 -->
                <transition-group move-class="move">
                    <div class="item" v-for="item in list" :key="item.id">{{item.num}}</div>
                </transition-group>
            </div>
        </div>
    </template>
    
    <style scoped lang='less'>
        .wrapper{
            display: flex;
            flex-wrap: wrap;
            width: calc(40px*9 + 9px);
            border-left:1px solid black ;
            border-top:1px solid black ;
            .item{
                border: 1px solid black;
                padding: 5px;
                width: 30px;
                line-height: 30px;
                text-align: center;
                border-left: none;
                border-top: none ;
            }
        }
        .move{
            transition: all 1s;
        }
    </style>
    
    • 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
    • 45
    • 46
    • 47

    16.4.3 状态过渡

    在这里插入图片描述

    npm install gsap
    
    • 1
    <script lang="ts" setup>
        import {reactive, ref, watch} from 'vue'
        import gsap from "gsap";
        import "animate.css"
        const num = reactive({
            current:0,
            newVal:0
        })
    
        // 监视当前的值是否发生变化
        watch(()=>num.current,(newVal,oldVal) =>{
            // 使用这个gasp去过渡,它可以接收一个对象
            gsap.to(num,{
                newVal:newVal,
                // 持续时间
                duration:3,
                // 过渡曲线
                ease:"espo.out"
            })
        })
    </script>
    
    <template>
        <div>
            <div>
                <input type="number" v-model="num.current" step="30">
                <transition-group>
                    <h2>{{num.newVal.toFixed()}}</h2>
                </transition-group>
            </div>
        </div>
    </template>
    
    <style scoped lang='less'>
    </style>
    
    • 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

    总结

    以上就是今天要讲的内容,希望对大家有所帮助!!!

  • 相关阅读:
    FreeRTOS自我救赎3之USB虚拟串口
    【从零开始学习 UVM】2.1、UVM 基础功能 —— Base Class
    SpringMVC笔记——SpringMVC配置类取代xml配置
    【06】指令跳转:原来if...else就是goto
    InnoDB备份与恢复篇(4)-InnoDB的故障恢复与日志分析
    CSS 表格
    Java 开发中的 Lombok 是什么?
    解决webstrom ERROR in [eslint] ESLint is not a constructor
    css:clip元素裁剪实现Loading加载效果边框
    SQL的 ISNULL 函数
  • 原文地址:https://blog.csdn.net/Trees__/article/details/126919371