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>
相关推荐
惜分飞3 分钟前
sql server 事务日志备份异常恢复案例---惜分飞
前端·数据库·php
GISer_Jing21 分钟前
WebGL实例化渲染:性能提升策略
前端·javascript·webgl
Gomiko31 分钟前
JavaScript进阶(四):DOM监听
开发语言·javascript·ecmascript
烟锁池塘柳035 分钟前
【技术栈-前端】告别“转圈圈”:详解前端性能优化之“乐观 UI” (Optimistic UI)
前端·ui
How_doyou_do1 小时前
浏览器本地存储Cookie, local/sessionStorage - Token结合Cookie实现登录管理
前端
syt_10131 小时前
grid布局之-子项放置4
开发语言·javascript·ecmascript
烛阴1 小时前
C# Dictionary 入门:用键值对告别低效遍历
前端·c#
spencer_tseng2 小时前
jquery download
javascript·jquery
极速蜗牛2 小时前
告别部署焦虑!PinMe:前端开发者的极简部署神器
前端·javascript
uhakadotcom3 小时前
Python Protobuf 全面教程:常用 API 串联与实战指南
前端·面试·github