$nextTick函数

vue中,模板解析会在methods中某一个方法的全部语句执行完毕之后进行重新解析

如:在某一方法中想获取一个输入框的焦点。恰好输入框的逻辑是隐藏的,在此次执行完毕之后才会暴露出来,那么获取焦点的操作就是无效的。

此处可以使用$nextTick(回调函数)函数解决

作用:在下一次DOM更新结束后执行其指定的回调函数

什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行

this.$nextTick(function () {
     this.$refs.inputTitle.focus()
});

同样可以使用定时器解决,不给定时器加时间

定时器不加时间时会推向队列执行

setTimeout(()=>{
	this.$refs.inputTitle.focus()
})

Vue动画效果

在模板中使用transition标签

需要在style中添加指定名称的样式,才能使动画生效

appear让动画在页面刷新时就会出现

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

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

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

  /*vue指定的进入动画名称*/
  .hello-enter-active{
    animation: atguigu 1s;
  }

  /*vue指定的退出动画名称*/
  .hello-leave-active{
    animation: atguigu 1s reverse;
  }

  @keyframes atguigu {
    from{
      transform: translateX(-100%);
    }
    to{
      transform: translateX(0px);
    }
  }
</style>

过渡效果

vue在动画过程中,会添加三个参数

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

<script>
  export default {
    name:'Test2',
    data(){
      return {
        isShow:true
      }
    },
  }
</script>

<style scoped>
  h1{
    background-color: orange;
  }
  /*进入的起点*/
  .hello-enter,.hello-leave-to{
    transform: translateX(-100%);
  }
  /*进入的终点*/
  .hello-enter-to,.hello-leave{
    transform: translateX(0);
  }
  .hello-enter-active,.hello-leave-active{
    transition: 0.5s linear;
  }
</style>

多个元素过渡

<transition-group>

其中的每个元素,都需要一个唯一的key值

<transition>只能作用域单一元素

第三方动画库

animate.css

npm install animate.css
<template>
  <div>
    <button @click="isShow=!isShow">显示/隐藏</button>
<!--    <h1 v-show="isShow" class="come">你好啊</h1>-->
    <transition-group
        appear
        name="animate__animated animate__bounce"
        enter-active-class="animate__swing"
        leave-active-class="animate__backOutUp"
    >
      <h1 v-show="!isShow" key="1">你好啊</h1>
      <h1 v-show="isShow" key="2">lanaln</h1>
    </transition-group>
  </div>
</template>

<script>
import 'animate.css'
  export default {
    name:'Test3',
    data(){
      return {
        isShow:true
      }
    },
  }
</script>

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

</style>