摸鱼少学习多

day86-动画与过度

过度与动画

使用vue写一些插入与更新或者移除dom元素时,在合适的时候使用元素动画

方法一

首先设计动画样式:

 @keyframes gugu {
   from{
     transform: translateX(-100%);
   }
   to{
     transform: translateX(0px);
   }
 }

 

使用enter和leave设计进入与离开的动画

 .hello-enter-active{
   animation: gugu 1s linear;
 }
 .hello-leave-active{
   animation: gugu 1s reverse;
 }

 

使用动画

 
复制代码
<template>
   <div>
     <button @click="isShow = !isShow">显示/隐藏</button>
 <!--    <transition name="hello" :appear="true">-->
     <transition name="hello" appear>
       <h1 v-show="isShow">hello!</h1>
     </transition>
   </div>
 </template>
复制代码

 

方法二

多个元素需要使用动画时

首先设计动画

复制代码
 /*进入的起点 离开的终点*/
 .hello-enter,.hello-leave-to{
   transform: translateX(-100%);
 }
 .hello-enter-active,.hello-leave-active{
   transition: 1s linear;
 }
 /*进入的终点 离开的起点*/
 .hello-enter-to,.hello-leave{
   transform: translateX(0);
 }
复制代码

 

使用transition-group包裹多个渲染元素

复制代码
 <template>
   <div>
     <button @click="isShow = !isShow">显示/隐藏</button>
 <!--    <transition name="hello" :appear="true">-->
     <transition-group name="hello" appear>
       <h1 v-show="isShow" key="1">hello!</h1>
       <h1 v-show="isShow" key="2">gugu!</h1>
     </transition-group>
   </div>
 </template>
复制代码

 

方法三

引入snimate.css包使用现成动画

 import 'animate.css'

 

在dom元素上设计动画,有一些固定的格式

复制代码
 <template>
   <div>
 <!--    <button @click="isShow = !isShow">显示/隐藏</button>-->
 <!--    <transition name="hello" :appear="true">-->
     <transition-group
         name="animate__animated animate__bounce"
         appear
         enter-active-class="animate__rubberBand"
         leave-active-class="animate__backOutUp"
     >
       <h1 v-show="isShow" key="2">hello!</h1>
     </transition-group>
     <transition
       name="animate__animated animate__bounce"
       appear
       enter-active-class="animate__heartBeat"
       leave-active-class=""
     >
       <h1 v-show="isShow" key="2">beautiful girl cmt</h1>
     </transition></div>
 </template>
复制代码

 

总结

 
复制代码
/*
   vue封装的过度与动画
     1.作用:在插入,更新或移除dom元素时,在合适的时候给元素添加样式类名
     2.写法:
       1.准备好样式:
         元素进入的样式:
           v-enter:进入的起点
           v-enter-active:进入过程中
           v-enter-to:进入的终点
         元素离开的样式:
           v-leave:离开的起点
           v-leave-active:离开过程中
           v-leave-to:离开的终点
         2.使用<transition>包裹要过度的元素,并配置name属性
         <transition name="hello" appear>
           <h1 v-show="isShow">hello!</h1>
         </transition>
         3.若有多个元素需要过度,则需要使用<transition-group>
           且每个元素需要指定key值
 */
复制代码

 

 
posted @   北海之上  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
/* 粒子吸附*/
点击右上角即可分享
微信分享提示