CSS 动画用法同 CSS 过渡,区别是在动画中 v-enter
类名在节点插入 DOM 后不会立即删除,而是在 animationend
事件触发时删除。
示例:(省略了兼容性前缀)
| <div id="example-2"> |
| <button @click="show = !show">Toggle show</button> |
| <transition name="bounce"> |
| <p v-if="show">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris facilisis enim libero, at lacinia diam fermentum id. Pellentesque habitant morbi tristique senectus et netus.</p> |
| </transition> |
| </div> |
| new Vue({ |
| el: '#example-2', |
| data: { |
| show: true |
| } |
| }) |
| .bounce-enter-active { |
| animation: bounce-in .5s; |
| } |
| .bounce-leave-active { |
| animation: bounce-in .5s reverse; |
| } |
| @keyframes bounce-in { |
| 0% { |
| transform: scale(0); |
| } |
| 50% { |
| transform: scale(1.5); |
| } |
| 100% { |
| transform: scale(1); |
| } |
| } |
| <!DOCTYPE html> |
| <html lang="zh"> |
| |
| <head> |
| <meta charset="UTF-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>CSS 动画</title> |
| <script src="./vue.js"></script> |
| <style> |
| .hzhBounce-enter-active { |
| animation: hzh-bounce-in .5s; |
| } |
| |
| .hzhBounce-leave-active { |
| animation: hzh-bounce-in .5s reverse; |
| } |
| |
| @keyframes hzh-bounce-in { |
| 0% { |
| transform: scale(0); |
| } |
| 50% { |
| transform: scale(1.5); |
| } |
| 100% { |
| transform: scale(1); |
| } |
| } |
| </style> |
| </head> |
| |
| <body> |
| <div id="hzh"> |
| <button @click="hzhShow = !hzhShow"> |
| 切换显示 |
| </button> |
| <transition name="hzhBounce"> |
| <p v-if="hzhShow"> |
| 黄子涵是帅哥!<br> |
| 黄子涵是靓仔!<br> |
| 黄子涵真厉害!<br> |
| 黄子涵真聪明! |
| </p> |
| </transition> |
| </div> |
| <script> |
| new Vue({ |
| el: '#hzh', |
| data: { |
| hzhShow: true |
| } |
| }) |
| </script> |
| </body> |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?