位置: 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)

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

  • 不再烧钱 细节入微也能提高搜索推广营销效果

    不再烧钱 细节入微也能提高搜索推广营销效果

  • s5500交换机(s5570)(s5500交换机 web)

    s5500交换机(s5570)(s5500交换机 web)

  • 小米10支持定位与导航有哪些(小米10手机有定位功能吗)

    小米10支持定位与导航有哪些(小米10手机有定位功能吗)

  • 微信如何迁移聊天记录(微信如何迁移聊天记录到新手机上)

    微信如何迁移聊天记录(微信如何迁移聊天记录到新手机上)

  • 苹果11怎么弄闹钟铃声(苹果11怎么弄闹钟)

    苹果11怎么弄闹钟铃声(苹果11怎么弄闹钟)

  • 天猫超市漏发货怎么办(天猫超市发货漏发)

    天猫超市漏发货怎么办(天猫超市发货漏发)

  • 小新14和小新15有什么区别(小新14和小新15哪个摄像头好用)

    小新14和小新15有什么区别(小新14和小新15哪个摄像头好用)

  • 天猫精灵待机耗电量吗(天猫精灵待机耗电大吗)

    天猫精灵待机耗电量吗(天猫精灵待机耗电大吗)

  • 华为有carplay功能么(华为支持carplay)

    华为有carplay功能么(华为支持carplay)

  • 华为nova7耳机插哪里(华为nova7耳机插上还是外放)

    华为nova7耳机插哪里(华为nova7耳机插上还是外放)

  • 手机没欠费用不了流量(手机没欠费用不了流量打哪个电话咨询)

    手机没欠费用不了流量(手机没欠费用不了流量打哪个电话咨询)

  • 小爱同学突然不能用了(小爱同学突然不能播放音乐了)

    小爱同学突然不能用了(小爱同学突然不能播放音乐了)

  • y5s什么时候上市(y5s什么时候上市的手机)

    y5s什么时候上市(y5s什么时候上市的手机)

  • 机顶盒能连电脑吗(机顶盒怎么连接电视)

    机顶盒能连电脑吗(机顶盒怎么连接电视)

  • ios11怎么一键清除通知(iphone11怎么一键清理缓存)

    ios11怎么一键清除通知(iphone11怎么一键清理缓存)

  • word2010怎么设置每页几行(word2010怎么设置自动保存时间间隔)

    word2010怎么设置每页几行(word2010怎么设置自动保存时间间隔)

  • 好玩吧账号可以注销吗(好玩吧能改名再次开放吗)

    好玩吧账号可以注销吗(好玩吧能改名再次开放吗)

  • 怎样把抖音视频锁起来(怎样把抖音视频播放长一点)

    怎样把抖音视频锁起来(怎样把抖音视频播放长一点)

  • p站官网怎么登进去(p站怎样登录)

    p站官网怎么登进去(p站怎样登录)

  • vivo_s1pro可以无线充电吗(vivo s1 pro支持nfc吗)

    vivo_s1pro可以无线充电吗(vivo s1 pro支持nfc吗)

  • 小米4手环怎么连接手机(小米4手环怎么放音乐?)

    小米4手环怎么连接手机(小米4手环怎么放音乐?)

  • 金立gn9012什么型号(金立gn9010是什么型号)

    金立gn9012什么型号(金立gn9010是什么型号)

  • 【计算机视觉】新冠肺炎COVID-19 CT影片阳性检测,感染区域分割,肺部分割,智慧医疗实践,医疗影像处理示例(计算机视觉需要学什么)

    【计算机视觉】新冠肺炎COVID-19 CT影片阳性检测,感染区域分割,肺部分割,智慧医疗实践,医疗影像处理示例(计算机视觉需要学什么)

  • 【HTML | CSS】纯CSS居然能做出这种效果,一款宝藏网页分享(超详细讲解 | 附源码)(css+html)

    【HTML | CSS】纯CSS居然能做出这种效果,一款宝藏网页分享(超详细讲解 | 附源码)(css+html)

  • 认证不过的进项税是怎么调出分录?
  • 纳税人期末存货怎么结转
  • 转出多交增值税和转出未交增值税怎么理解
  • 增票未抵扣丢失怎么处理
  • 饭店采购食材没发票
  • 已认证红冲需要退回发票吗
  • 第二季度的季初资产总额和季末资产总额怎么填
  • 金蝶以前年度损益调整属于哪类科目
  • 异地开发房地产会一直待在那个城市吗
  • 如何去办理小型微利企业资格每年都需要认证吗?
  • 应收票据贴现的含义
  • 固定资产原值增加当月计提折旧吗
  • 收到电费发票做账摘要怎么写
  • 房地产企业内部查账查什么
  • 代收水电气费加盟
  • 企业印花税算法
  • 增值税普票税额
  • 小规模纳税人简易计税方法
  • 三证合一后有效期多久
  • 进项票已认证忘记开票
  • 普通发票需要什么
  • 公司账上亏损
  • 存货成本包括哪些项目
  • 主营业务成本会计科目使用说明
  • 天然气税费
  • 盈余公积转增实收资本要交税吗
  • 销售净利率如何分析出来
  • 社保多交怎么办理
  • 公司购买的财产保险服务可以抵扣进项吗
  • 经营出租固定资产折旧额计入什么科目
  • 结转人工费会计分录
  • 消费税和购置税怎么算
  • 发票认证平台上不去
  • 鸿蒙系统怎么设置双击亮屏
  • mac 查看当前用户
  • laravel 分层
  • 购货返利
  • laravel创建项目
  • 核定征收印花税的文件
  • 应收账款账务处理及案例分析
  • 星空下的灯塔作文
  • win11大小核调度会优化吗
  • 用python编写
  • idea连接sqlserver数据库教程
  • 新个税讲解
  • php静态方法可以被继承吗
  • wordpress mobile themes
  • 行政事业单位个税代扣怎么记账
  • 织梦cms为什么不维护了
  • mongodb查询字段不存在
  • 织梦使用教程
  • 冲以前年度管理费用
  • 代征税款手续费规定
  • 送货单和收款收据的区别
  • 捐赠支出怎么抵税
  • 往来款和货款
  • 法人一直把公户账户转账
  • 单位发放职工2000元慰问金申请怎么写
  • 工厂厨房厨具
  • 库存现金的主要活动
  • 未开票收入账上税率按多少
  • 招待费可以做成什么科目
  • 公益捐赠仪式流程
  • 明细账示例图
  • 个体工商户如何给员工交五险一金
  • mysql事件id100
  • mysql进阶之路
  • window7 aero
  • win7防火墙怎么彻底关闭
  • linux中keepalive
  • linux卸载repo
  • div +css
  • 巧用dos命令合并图层
  • angular.js
  • unity制作的2d游戏
  • 刀具路径轨迹模拟
  • javascript里的yield
  • 纸质发票怎么查电子发票
  • 生鲜肉类免税
  • 陕西个体户免税政策
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设