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

CSS Transitions 过渡效果详解

CSS Transitions 过渡效果详解

一、Transitions 概述

CSS Transitions(过渡)用于在元素状态变化时创建平滑的动画效果。它们允许属性值在一段时间内平滑过渡。

1.1 基本语法

.element { transition: property duration timing-function delay; }

二、过渡属性

2.1 transition-property

/* 单个属性 */ .element { transition-property: width; } /* 多个属性 */ .element { transition-property: width, height, opacity; } /* 所有属性 */ .element { transition-property: all; }

2.2 transition-duration

.element { transition-duration: 0.3s; transition-duration: 500ms; }

2.3 transition-timing-function

/* 线性 */ .timing-linear { transition-timing-function: linear; } /* 缓入 */ .timing-ease-in { transition-timing-function: ease-in; } /* 缓出 */ .timing-ease-out { transition-timing-function: ease-out; } /* 缓入缓出 */ .timing-ease-in-out { transition-timing-function: ease-in-out; } /* 自定义贝塞尔曲线 */ .timing-custom { transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); } /* 步进 */ .timing-steps { transition-timing-function: steps(4); }

2.4 transition-delay

.element { transition-delay: 0.2s; }

三、组合写法

3.1 简写形式

/* 完整写法 */ .element { transition: all 0.3s ease-in-out 0.1s; } /* 多个属性 */ .element { transition: width 0.3s ease, height 0.5s ease-out; }

四、可过渡属性

4.1 常用可过渡属性

.element { /* 尺寸 */ width: 100px; height: 100px; padding: 10px; /* 颜色 */ color: #333; background-color: #fff; border-color: #ccc; /* 位置 */ transform: translateX(0); left: 0; top: 0; /* 透明度 */ opacity: 1; /* 边框 */ border-width: 1px; border-radius: 4px; /* 阴影 */ box-shadow: 0 2px 4px rgba(0,0,0,0.1); }

五、实战案例

5.1 悬停效果

.button { padding: 12px 24px; background: #667eea; color: white; border-radius: 8px; transition: all 0.3s ease; } .button:hover { background: #764ba2; transform: translateY(-2px); box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4); }

5.2 卡片翻转

.card { transition: transform 0.6s ease; transform-style: preserve-3d; } .card.flipped { transform: rotateY(180deg); }

5.3 进度条动画

.progress-bar { height: 10px; background: #eee; border-radius: 5px; overflow: hidden; } .progress-bar::after { content: ''; display: block; width: 0; height: 100%; background: linear-gradient(90deg, #667eea, #764ba2); transition: width 1s ease-out; } .progress-bar.active::after { width: 75%; }

5.4 菜单展开

.menu { max-height: 0; overflow: hidden; transition: max-height 0.3s ease; } .menu.open { max-height: 500px; }

5.5 图片悬停缩放

.image-container { overflow: hidden; border-radius: 12px; } .image-container img { width: 100%; height: auto; transition: transform 0.4s ease; } .image-container:hover img { transform: scale(1.1); }

六、性能考虑

6.1 触发重绘/回流

/* 触发回流(不推荐) */ .element { transition: width 0.3s; } /* 仅触发合成(推荐) */ .element { transition: transform 0.3s; }

6.2 will-change

.element { will-change: transform, opacity; }

七、JavaScript 触发过渡

7.1 添加/移除类

const element = document.querySelector('.box'); // 添加过渡 element.classList.add('active'); // 移除过渡 element.classList.remove('active');

7.2 监听过渡结束

element.addEventListener('transitionend', (event) => { console.log('过渡完成:', event.propertyName); });

八、浏览器兼容性

属性ChromeFirefoxSafariIE
transition26+16+6.1+10+

九、总结

CSS Transitions 是创建平滑动画的基础:

  1. 过渡属性- property、duration、timing-function、delay
  2. 可过渡属性- 尺寸、颜色、位置、透明度等
  3. 性能优化- 使用 transform 和 opacity
  4. JavaScript 集成- 触发过渡、监听结束事件

合理使用过渡可以提升用户体验。

http://www.gsyq.cn/news/1424338.html

相关文章:

  • Claude生成代码质量究竟如何?37项实测指标揭穿90%开发者忽略的隐藏风险
  • 【雷达干扰】FMCW 雷达稀疏低秩 Hankel 矩阵分解的干扰抑制附Matlab代码
  • 2026年近期,如何选择行业知名的液压马达定制厂家? - 2026年企业资讯
  • 隐形冠军舜展智能:16年磨一剑,用等离子技术点亮中国高端制造
  • 第19篇|沉浸式首页:地图、玻璃层、信息卡片的层级关系
  • 16位ADC不够用?别急着换芯片!教你用“过采样+滑动平均”榨出24位极致精度
  • 高性能语音合成部署:基于Sherpa-Onnx的MeloTTS多语言模型转换与优化方案
  • 文泉驿微米黑终极安装指南:5MB轻量级中文字体跨平台快速部署
  • 别只用来聊天!解锁BitoAI在VSCode中的5个高效编程场景(含代码规范检查与性能优化)
  • 给Kali 2022.1换张‘脸’:从默认主题到中文界面,一次搞定所有视觉和语言设置
  • CSS View Transitions API 详解
  • Realtek蓝牙鼠标卡顿?别急着换硬件,试试这个被忽略的Windows后台服务优化
  • 手把手教你玩转CST材料库:导入厂家数据、创建自定义吸波材料全攻略
  • 合肥本地招聘为什么首选合肥直聘兔?本土优势+真实数据+落地案例详解 - drfdxr
  • RVC-WebUI:5分钟掌握AI语音克隆的完整指南
  • 2026年美国DOE认证检测机构权威排行一览:IEC60825检测、加州CEC认证、欧盟ERP认证、激光CE认证选择指南 - 优质品牌商家
  • Ubuntu 18.04下Tesla M40显卡驱动安装避坑实录:从‘NVIDIA-SMI失败’到‘Above 4G Decoding’的正确姿势
  • Flutter GoRouter 路由导航详解
  • 网盘直链解析终极指南:一键解锁高速下载体验
  • 如何快速解锁QQ音乐加密文件:qmcdump音频解密工具终极指南
  • 2026年横评10款降AI率工具:一键锁定高效助手! - 降AI小能手
  • 179、运动控制中的行业标准:机器人安全标准ISO 10218
  • 告别PuTTY!Win10/Server 2019自带的OpenSSH客户端,这样用才顺手
  • 【Agent智能体10 | 反思设计模式-AI数据分析的可视化实战】
  • 别再死磕Vivado了!用VSCode写ZYNQ代码,效率翻倍的保姆级配置指南
  • 多 Agent 对证循环协作架构:Hermes + Claude Code + Codex 三角色工作流实战
  • WarcraftHelper魔兽争霸III优化工具:5分钟解锁游戏全部潜力,告别老旧限制
  • Arm CCI-550/CCI-500地址通道带宽异常分析与优化
  • 这6个动作让python selenium爬虫规避检测
  • 【Claude NPV分析权威指南】:20年财务AI专家首曝大模型估值新范式,3步精准测算项目真实价值