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>
相关推荐
lightgis1 小时前
16openlayers加载COG(云优化Geotiff)
前端·javascript·html·html5
小飞大王6661 小时前
TypeScript核心类型系统完全指南
前端·javascript·typescript
你的人类朋友3 小时前
✍️记录自己的git分支管理实践
前端·git·后端
合作小小程序员小小店3 小时前
web网页开发,在线考勤管理系统,基于Idea,html,css,vue,java,springboot,mysql
java·前端·vue.js·后端·intellij-idea·springboot
防火墙在线3 小时前
前后端通信加解密(Web Crypto API )
前端·vue.js·网络协议·node.js·express
Jacky-0083 小时前
Node + vite + React 创建项目
前端·react.js·前端框架
CoderYanger4 小时前
前端基础——CSS练习项目:百度热榜实现
开发语言·前端·css·百度·html·1024程序员节
i_am_a_div_日积月累_4 小时前
10个css更新
前端·css
她是太阳,好耀眼i5 小时前
Nvm 实现vue版本切换
javascript·vue.js·ecmascript
蒲公英10015 小时前
在wps软件的word中使用js宏命令设置表格背景色
javascript·word·wps