当前位置: 首页 > article >正文

Vue3新组件transition(动画过渡)

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

1.基本使用 

 

<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>
<script>
const falg = ref(true)
</script>

 样式动画

<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的动画

<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.小案例-按钮切换滚动

<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依赖包

npm install animate.css -S
yarn add animate.css -S

 

 4.2在使用的组件中导入

import 'animate.css';

 4.3结合transition使用animate动画库

<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>

 


http://www.kler.cn/news/315490.html

相关文章:

  • Java API 之集合框架进阶
  • 软件测试面试题(5)——二面(游戏测试)
  • 【PLW003】设备器材云端管理平台v1.0(SpringBoot+Mybatis+NodeJS+MySQL前后端分离)
  • LeetCode题练习与总结:回文链表--234
  • [JavaEE]———进程、进程的数据结构、进程的调度
  • 【优选算法之二分查找】No.5--- 经典二分查找算法
  • Linux之实战命令03:stat应用实例(三十七)
  • 如何使用 maxwell 同步到 redis?
  • 如何在 CentOS 中管理用户、组和服务状态
  • git pull的merge和rebase模式
  • Spring解决循环依赖的原理
  • RuntimeError: Maximum Recursion Depth Exceeded - 递归深度超限的完美解决方案
  • Spring 源码分析
  • C++独立开发开源大数计算库 CBigNum
  • MySQL之内置函数
  • 【笔记】第三节 组织与性能
  • 搜维尔科技:Unity中的A.R.T.测量工具
  • 金仓数据库 KingbaseES参考手册 (8. 函数(九))
  • C++标准库容器类——string类
  • KTH5762系列 低功耗、高精度 3D 霍尔角度传感器 电子手表旋钮应用
  • 机器翻译之Bahdanau注意力机制在Seq2Seq中的应用
  • 【计网】从零开始掌握序列化 --- JSON实现协议 + 设计 传输\会话\应用 三层结构
  • 对时间序列SOTA模型Patch TST核心代码逻辑的解读
  • 基于区块链的相亲交易系统源码解析
  • vue3 本地windows下的字体的引用
  • 分布式锁优化之 使用lua脚本改造分布式锁保证判断和删除的原子性(优化之LUA脚本保证删除的原子性)
  • FFmpeg开发笔记(五十六)使用Media3的Exoplayer播放网络视频
  • Java 入门指南:JVM(Java虚拟机)——类的生命周期与加载过程
  • web基础—dvwa靶场(八)SQL Injection(Blind)
  • 众数信科AI智能体政务服务解决方案——寻知智能笔录系统