Vue3新组件transition(动画过渡)

transition组件:控制V-if与V-show的显示与隐藏动画

1.基本使用

html 复制代码
<template>
  <div>
    <button @click="falg = !falg">切换</button>
    <transition name="fade" :enter-to-class="etc">
      <div v-if="falg" class="box ">box1</div>
      <div v-else class="box2">box2</div>
    </transition>
  </div>
</template>
javascript 复制代码
<script>
const falg = ref(true)
</script>

样式动画

css 复制代码
<style scoped>
.box {
  width: 200px;
  height: 200px;
  text-align: center;
  background-color: red;
  line-height: 200px;

}

.box2 {
  text-align: center;
  line-height: 200px;
  width: 200px;
  height: 200px;
  color: white;
  background-color: black;
}
/* 插入的新元素初始的样式 */
.fade-enter-from {
  width: 0;
  height: 0;
  color: white;
}

.fade-enter-active,
.fade-leave-active {
  transition: all 5s ease;
}

/* 出入的新的元素的结束的样式 */
.fade-enter-to {
  width: 200px;
  height: 200px;
  transform: translateY(-200px);
  /* background: black; */
}
/* 移除旧元素的结束的样式 */
.fade-leave-to {
  width: 200px;
  height: 200px;
  background: white;
}
</style>

2.appear属性

在进入页面时就会触发enter-from到enter-to的动画

html 复制代码
<template>
  <div>
    <button @click="falg = !falg">切换</button>
    <transition appear name="fade" :enter-to-class="etc">
      <div v-if="falg" class="box ">box1</div>
      <div v-else class="box2">box2</div>
    </transition>
  </div>
</template>

3.小案例-按钮切换滚动

html 复制代码
<script setup>
import { ref } from 'vue'

const docState = ref('saved')
</script>

<template>
	<span style="margin-right: 20px">Click to cycle through states:</span>
  <div class="btn-container">
		<Transition name="slide-up">
      <button v-if="docState === 'saved'"
              @click="docState = 'edited'">Edit</button>
      <button v-else-if="docState === 'edited'"
              @click="docState = 'editing'">Save</button>
      <button v-else-if="docState === 'editing'"
              @click="docState = 'saved'">Cancel</button>
    </Transition>
  </div>
</template>

<style>
.btn-container {
  display: inline-block;
  position: relative;
  height: 1em;
}

button {
  position: absolute;
}

.slide-up-enter-active,
.slide-up-leave-active {
  transition: all 0.25s ease-out;
}

.slide-up-enter-from {
  opacity: 0;
  transform: translateY(30px);
}

.slide-up-leave-to {
  opacity: 0;
  transform: translateY(-30px);
}
</style>

4.配合animate.css动画库使用

4.1下载安装animate依赖包

css 复制代码
npm install animate.css -S
javascript 复制代码
yarn add animate.css -S

4.2在使用的组件中导入

javascript 复制代码
import 'animate.css';

4.3结合transition使用animate动画库

html 复制代码
<script setup>
import 'animate.css';
const falg = ref(true)
</script>
<template>
  <div style="margin-top: 500px;">
    <button @click="falg = !falg">切换</button>
    <transition name="fade" :enter-to-class="etc">
      <h1 v-if="falg">
        <div class="box animate__animated animate__bounce">你好</div>
      </h1>
      <h1 v-else>
        <div class="box2 animate__animated animate__fadeInLeft">你好</div>
      </h1>
    </transition>
  </div>
  <div class="box2 animate__animated animate__hinge">你好</div>
</template>
<style scoped>
.box {
  width: 200px;
  height: 200px;
  text-align: center;
  background-color: red;
  line-height: 200px;

}

.box2 {
  text-align: center;
  line-height: 200px;
  width: 200px;
  height: 200px;
  color: white;
  background-color: black;
}

</style>
相关推荐
LabVIEW开发7 分钟前
将自定义 DLL 与 INI 文件部署到 LabVIEW 实时目标
前端·labview·labview知识·labview功能·labview程序
IT_陈寒21 分钟前
Redis Pipeline用错竟比不用还慢,这个坑我帮你踩过了
前端·人工智能·后端
ynchyong34 分钟前
JavaScript Promise 实战:.then() 与 .catch() 的区别及回调函数封装指南
开发语言·javascript·ecmascript·promise·then
大模型码小白37 分钟前
数据可视化:AI处理多维数据的HTML5可视化方案
大数据·前端·javascript·人工智能·机器学习·信息可视化·html5
LabVIEW开发1 小时前
LabVIEW 前面板装饰图形的格式选择与自定义选板添加
前端·labview·labview知识·labview功能·labview程序
掘金者阿豪1 小时前
GPT-6 Astra 来了,GPT-5.6 Sol 还值得用吗?聊聊 Coding、百万上下文、价格和 Plus/Pro
前端·后端
code 小楊1 小时前
腾讯开源 WeKnora 深度解析:RAG 问答 + ReAct Agent 推理 + 自动 Wiki 图谱,三位一体的企业级知识中台
前端·人工智能·开源·知识图谱
roamingcode1 小时前
让 AI 的回答「逐段说话」:react-streaming 的顺序流式渲染实践
前端·人工智能·react.js·codex
BioRunYiXue1 小时前
科研干货 | IC50全面解读:概念解析、实验设计与数据分析要点
java·开发语言·javascript·人工智能·算法·数据挖掘·数据分析
cidy_981 小时前
Main 正式环境合并说明
前端