vue CLI_6、$nextTick(生命周期钩子)、过度与动画

1、nextTick

vue里特殊的生命周期钩子。

  1. 语法:this.$nextTick(回调函数)
  2. 作用:在下一次 DOM 更新结束后执行其指定的回调。
  3. 什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行。

比如:修改了vm的data中的数据要对页面上的dom元素做出马上修改时。

2、Vue封装的过度与动画

  1. 作用:在插入、更新或移除 DOM元素时,在合适的时候给元素添加样式类名。

  2. 图示:
    image

  3. 写法:

    1. 准备好样式:

      • 元素进入的样式:
        1. v-enter:进入的起点
        2. v-enter-active:进入过程中
        3. v-enter-to:进入的终点
      • 元素离开的样式:
        1. v-leave:离开的起点
        2. v-leave-active:离开过程中
        3. v-leave-to:离开的终点
    2. 使用<transition>包裹要过度的元素,并配置name属性:

      <transition name="hello">
      	<h1 v-show="isShow">你好啊!</h1>
      </transition>
      
    3. 备注:若有多个元素需要过度,则需要使用:<transition-group>,且每个元素都要指定key值。

4,要让页面一开始就显示动画,需要添加apper

src\App.vue


<template>
<div id="root">
  <Test/>
  <Test2/>
  <Test3/>
</div>
    
</template>

<script>
    //引入组件
    import Test from "./components/Test.vue";
    import Test2 from "./components/Test2.vue";
    import Test3 from "./components/Test3.vue";
    


    export default {
        name:"App",
        components:{ 
          Test,
          Test2,
          Test3        
        },
        data(){
            return{
            }
        },
    }

</script>

src\components\Test.vue使用动画

<template>
<div>
    <button @click="isShow=!isShow">显示/隐藏</button>
    <transition name="hello" :appear="true">
        <h1 v-show="isShow" >你好啊!</h1>    
    </transition>
    
</div>
  
</template>

<script>
export default {
    name:"Test",
    data(){
        return{
            isShow:true
        }
    },
    methods:{
    }

}
</script>

<style scoped>
h1{
    background-color:aqua;
}

/* 动画进入时激活 */
.hello-enter-active{
    animation: fhzm 0.5s linear;
}

/* 动画离开时激活 */
.hello-leave-active{
    animation: fhzm 0.2s reverse;
}

/* 封装动画 */
@keyframes fhzm{
    from{
        transform: translateX(-100%);
    }
    to{
        transform: translateX(0px);
    }
}
</style>

src\components\Test2.vue使用过度

<template>
<div>
    <button @click="isShow=!isShow">显示/隐藏</button>
    <transition-group name="hello" :appear="true">
        <h1 v-show="isShow" key="1" >你好啊!</h1>   
        <h1 v-show="isShow" key="2" >你好啊!2</h1>     
    </transition-group>
    
</div>
  
</template>

<script>
export default {
    name:"Test",
    data(){
        return{
            isShow:true
        }
    },
    methods:{
    }

}
</script>

<style scoped>
h1{
    background-color:rgb(219, 206, 27);
}

/* 封装过度 */

/* 进入的起点 ,离开的终点 */
.hello-enter , .hello-leave-to{
    transform: translateX(-100%);
    /* transition: 0.2s linear; */
}
/* 进入的终点 ,离开的起点*/
.hello-enter-to,.hello-leave{
    
    transform: translateX(0);
    /* transition: 0.3s linear; */
}

.hello-enter-active , .hello-leave-active{
    transition: 0.2s linear;
}


</style>

src\components\Test3.vue使用第三方动画库animate库,在npmjs官网里可以搜到

<template>
<div>
    <button @click="isShow=!isShow">显示/隐藏</button>
    <transition-group 
    :appear="true"
    name="animate__animated animate__bounce"
    enter-active-class="animate__swing"
    leave-active-class="animate__backOutDown"
    >
        <h1 v-show="isShow" key="1" >你好啊!</h1>   
        <h1 v-show="isShow" key="2" >你好啊!2</h1>     
    </transition-group>
    
</div>
  
</template>

<script>

    //好用的第三方库https://www.npmjs.com/ 里的animate库

    //引入css样式
    import 'animate.css';

export default {
    name:"Test",
    data(){
        return{
            isShow:true
        }
    },
    methods:{
    }

}
</script>

<style scoped>
h1{
    background-color:rgb(226, 98, 130);
}


</style>

image

posted @ 2022-04-10 21:55  青仙  阅读(107)  评论(0编辑  收藏  举报