位置: IT常识 - 正文

Vue3【Transition(效果、CSS 过渡、使用animation、TransitionGroup、 KeepAlive、Teleport )】(七)-全面详解(学习总结---从入门到深化)

编辑:rootadmin
Vue3【Transition(效果、CSS 过渡、使用animation、TransitionGroup、 KeepAlive、Teleport )】(七)-全面详解(学习总结---从入门到深化)

推荐整理分享Vue3【Transition(效果、CSS 过渡、使用animation、TransitionGroup、 KeepAlive、Teleport )】(七)-全面详解(学习总结---从入门到深化),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

👏作者简介:大家好,我是小童,Java开发工程师,CSDN博客博主,Java领域新星创作者 📕系列专栏:前端、Java、Java中间件大全、微信小程序、微信支付、若依框架、Spring全家桶 📧如果文章知识点有错误的地方,请指正!和大家一起学习,一起进步👀 🔥如果感觉博主的文章还不错的话,请👍三连支持👍一下博主哦 🍂博主正在努力完成2023计划中:以梦为马,扬帆起航,2023追梦人

目录

​Transition效果

Transition CSS 过渡

 Transition 使用animation

TransitionGroup

 KeepAlive

Teleport 


Transition效果

Vue 提供了内置组件,可以帮助你制作基于状态变化的过渡和动画< Transition >  会在一个元素或组件进入和离开 DOM 时应用动画< Transition >  是一个内置组件,这意味着它在任意别的组件中都可以被使用,无需注册  

 过渡效果

<template> <button @click="show = !show">Toggle</button> <Transition> <p v-if="show">hello</p> </Transition></template><script setup> import { ref } from "vue" const show = ref(true)</script><style scoped>.v-enter-active, .v-leave-active { transition: opacity 0.5s ease;}.v-enter-from, .v-leave-to { opacity: 0;}.v-enter-to, .v-leave-from{ opacity: 1;}</style>Transition CSS 过渡

一共有 6 个应用于进入与离开过渡效果的 CSS class

<template> <button @click="changeHandler">变化</button> <Transition> <div v-if="show" class="box"></div> </Transition></template><script setup> import { ref } from "vue" const show = ref(true) function changeHandler(){ show.value = !show.value }</script><style scoped>.box{ width: 200px; height: 200px; background-color: red;}.v-enter-from{ opacity: 0;}.v-enter-active { transition: opacity 2s ease;}.v-enter-to { opacity: 1;}.v-leave-from{ opacity: 1;}.v-leave-active{ transition: opacity 2s ease;}.v-leave-to{ opacity: 0;}</style>

 为过渡效果命名

<template> <button @click="changeHandler">变化</button> <Transition name="box"> <div v-if="show" class="box"></div> </Transition></template><script setup>import { ref } from "vue"const show = ref(true)function changeHandler(){ show.value = !show.value}</script><style scoped> .box{ width: 200px; height: 200px; background-color: red; } .box-enter-from{ opacity: 0; } .box-enter-active { transition: opacity 2s ease; } .box-enter-to { opacity: 1; } .box-leave-from{ opacity: 1; } .box-leave-active{ transition: opacity 2s ease; } .box-leave-to{ opacity: 0; }</style>

实时效果反馈

1. 在Vue添加动画时,下列那个是 enter 过程动画的结束状态:

A v-enter-from

B v-enter-active

C v-enter-to

Vue3【Transition(效果、CSS 过渡、使用animation、TransitionGroup、 KeepAlive、Teleport )】(七)-全面详解(学习总结---从入门到深化)

D v-leave-to

 Transition 使用animation

使用 animation 实现动画效果 

<template> <button @click="changeHandler">变化</button> <Transition name="box"> <div v-if="show" class="box"></div> </Transition></template><script setup> import { ref } from "vue" const show = ref(true) function changeHandler() { show.value = !show.value }</script><style scoped> .box{ width: 200px; height: 200px; background-color: red;}.box-enter-active { animation: bounce-in 0.5s;}.box-leave-active { animation: bounce-in 0.5s reverse;}@keyframes bounce-in { 0% { transform: scale(0); } 50% { transform: scale(1.25); } 100% { transform: scale(1); }}</style>TransitionGroup

< TransitionGroup > 是一个内置组件,用于对 v-for 列表中的元素或组件的 插入、移除和顺序改变添加动画效果  

<template> <button @click="addHandler">增加</button> <TransitionGroup name="list" tag="ul"> <li v-for="item in items" :key="item"> {{ item }} </li> </TransitionGroup></template><script setup> import { reactive } from "vue" const items = reactive(["iwen","ime","frank"]) function addHandler() { items.push("sakura") }</script><style scoped> .list-enter-active, .list-leave-active { transition: all 0.5s ease; } .list-enter-from, .list-leave-to { opacity: 0; transform: translateX(30px); } .list-enter-to, .list-leave-from { opacity: 1; }</style> KeepAlive

在切换时创建新的组件实例通常是有意义的,但在这个例子中,我们的确想要组件能在被“切走”的时候保留它们的状态。要解决这个问题,我们可以用 < KeepAlive > 内置组件将这些动态组件包装起来  

保存状态 

<template> <button @click="changeHandler">切换</button> <keep-alive> <component :is="activeComponent"></component> </keep-alive></template><script> import { ref } from "vue" import ComponentA from "./ComponentA.vue" import ComponentB from "./ComponentB.vue" export default { components:{ ComponentA, ComponentB }, setup() { const activeComponent = ref("ComponentA") function changeHandler() { activeComponent.value = activeComponent.value === "ComponentA" ? "ComponentB" : "ComponentA" } return { activeComponent, changeHandler } }}</script><template> <h3>ComponentA</h3> <button @click="addCount">countA:{{ count }}</button></template><script setup> import { ref } from "vue" const count = ref(0) function addCount(){ count.value++ }</script><template> <h3>ComponentB</h3> <button @click="addCount">countA:{{ count }}</button></template><script setup> import { ref } from "vue" const count = ref(0) function addCount() { count.value++ }</script>

包含/排除

< KeepAlive > 默认会缓存内部的所有组件实例,但我们可以通过 include 和 exclude prop 来定制该行为

<template> <button @click="changeHandler">切换</button> <keep-alive include="ComponentB"> <component :is="activeComponent"></component> </keep-alive></template>

 缓存实例的生命周期

<template> <h3>ComponentB</h3> <button @click="addCount">countA:{{ count }}</button></template><script setup> import { ref,onActivated, onDeactivated } from "vue" const count = ref(0) function addCount() { count.value++ } onActivated(() => { // 调用时机为首次挂载 // 以及每次从缓存中被重新插入时 console.log("加入"); }) onDeactivated(() => { // 在从 DOM 上移除、进入缓存 // 以及组件卸载时调用 console.log("移除"); })</script>

实时效果反馈

1. 在Vue组件中,下列那个是缓存生命周期函数:

A created

B mounted

C updated

D activated

Teleport 

< Teleport >是一个内置组件,它可以将一个组件内部的一部分模板“传 送”到该组件的 DOM 结构外层的位置去

有时我们可能会遇到这样的场景:一个组件模板的一部分在逻辑上 从属于该组件,但从整个应用视图的角度来看,它在 DOM 中应该 被渲染在整个 Vue 应用外部的其他地方

< Teleport >提供了一个更简单的方式来解决此类问题,让我们不需要 再顾虑 DOM 结构的问题  

<template> <button @click="open = true">Open Modal</button> <Teleport to="body"> <div v-if="open" class="modal"> <p>Hello from the modal!</p> <button @click="open = false">Close</button> </div> </Teleport></template><script setup> import { ref } from 'vue' const open = ref(false)</script> <style scoped> .modal { position: fixed; z-index: 999; top: 20%; left: 50%; width: 300px; margin-left: -150px; background: #999; padding: 20px; text-align: center;} </style>
本文链接地址:https://www.jiuchutong.com/zhishi/299989.html 转载请保留说明!

上一篇:15000 字的 SQL 语句大全 第一部分(sql1000*1.0)

下一篇:微信小程序对上传的图片进行裁剪(微信小程序上面有个音乐怎么关闭)

  • 小规模纳税人可以开9%专票吗
  • 个体户怎么网上报税流程
  • 社保税款所属期限什么意思
  • 蔬菜坏了还能吃吗
  • 新会计准则施行
  • 派发现金红利分录
  • 报废过期产品怎么做会计分录
  • 个人借款利息是多少才合法
  • 材料成本会计的主要内容
  • 租地协调费没发票怎么入账?
  • 收到政府中小企业发票
  • 以银行存款退还投资者股金
  • 母公司不经营分公司能报税吗?
  • 展厅门口如何布置图片
  • 代开专票退票流程及说明
  • 园林绿化税收减免政策
  • 稳岗补贴需要缴税吗
  • 税法关于印花税的规定
  • 抵免税款收益算其他收益吗
  • 分公司可以单独签协议吗
  • 减免增值税可以税前扣除吗
  • 无票收入冲回会计分录
  • 固定资产报废后怎么处理
  • 缴纳车船税的车辆
  • 软件产品合同
  • 拆迁公司属于什么性质
  • 新入职员工如何加入企业微信
  • 返利红字发票怎么开具
  • php apc
  • 纳税人代扣代缴
  • 事业单位财政专项资金可以用于职工社保
  • 房地产企业现金流管理问题研究
  • php单链表使用场景
  • python深拷贝与浅拷贝区别
  • 进出口企业需要英文公章吗
  • 社会团体费用报销制度
  • 业务活动成本和管理费用能不能写在一起
  • 个体户和公司的税收相差多少
  • 大货车怎样申请报废
  • 固定资产接受捐赠的条件
  • 水利建设基金应税项是什么
  • 库存现金太多违反什么规定
  • 交强险和车船税在哪里买
  • 运输费用如何做账
  • 贷款减值准备如何计算
  • 加盖发票专用章有效什么意思
  • 库存不够如何结算成本
  • 支付投资者投入的款项
  • 资本公积只能转现金吗
  • 专票遗失没有认证能用吗
  • 小微企业即征即退
  • 小规模纳税人残保金减免政策
  • 合同取得成本如何收回
  • 成本费用会计科目
  • 预支款怎么做现金账
  • 产品是如何产生的
  • 数码相机无线连接手机
  • centos 界面安装
  • win8系统应用软件颜色不正常怎么办
  • win10调小键盘
  • linux在云计算中的使用
  • window10英文版下载
  • pim架构
  • linux使用shell脚本创建文件
  • 腙基是什么
  • Tree、Unformat、Vsafe命令的区别与使用说明
  • js正则 \w
  • vue $route $router
  • AJAX和jQuery动态加载数据的实现方法
  • node.js中的http.response.addTrailers方法使用说明
  • Python编程给定a=1,b=4,输出a+b的值
  • 读取带敏感字符的行的批处理
  • 怎样在手机上查询社保缴费情况
  • 天气球球下载
  • javascript 作用
  • android 引用第三方库
  • 企业自产自销农产品免税政策有哪些
  • 怎样把短信转发到微信
  • 贵州省税务局领导介绍
  • 铁力杯四省冠军
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设