答案:控制CSS动画中高度和宽度变化需注意性能、auto值处理、box-sizing影响及缓动函数选择。关键在于避免直接动画height/width引发的重排,推荐用transform: scale()提升性能;应对height: auto时可采用max-height配合overflow: hidden实现平滑展开;同时合理设置box-sizing确保尺寸预期一致,并通过cubic-bezier等缓动函数增强视觉流畅性,在手风琴、模态框、骨架屏等场景中实现自然的布局动态效果。

通过CSS
animation
@keyframes
height
width
transition
要实现通过
animation
@keyframes
height
width
/* 定义一个名为 'sizeChange' 的动画 */
@keyframes sizeChange {
0% {
width: 100px;
height: 50px;
background-color: lightblue;
}
50% {
width: 200px;
height: 100px;
background-color: coral;
}
100% {
width: 150px;
height: 75px;
background-color: lightgreen;
}
}
/* 将动画应用到某个元素 */
.animated-box {
width: 100px;
height: 50px;
background-color: lightblue;
border: 1px solid #333;
margin: 20px;
/* 应用动画 */
animation-name: sizeChange; /* 动画名称 */
animation-duration: 4s; /* 动画持续时间 */
animation-timing-function: ease-in-out; /* 动画速度曲线 */
animation-iteration-count: infinite; /* 动画播放次数,infinite表示无限循环 */
animation-direction: alternate; /* 动画方向,alternate表示来回播放 */
/* animation-fill-mode: forwards; */ /* 动画结束后保持最后一帧状态 */
}这段代码定义了一个名为
sizeChange
animation
keyframes
当我们决定用
@keyframes
height
width
立即学习“前端免费学习笔记(深入)”;
首先,性能开销是绕不开的话题。直接动画
height
width
transform: scale()
opacity
transform: scale()
其次,auto
height: 0
height: auto
height: auto
0
auto
auto
max-height
max-height
0
1000px
overflow: hidden
auto
/* 针对从0到auto的展开效果 */
.expand-box {
height: 0; /* 初始高度为0 */
max-height: 0; /* 配合max-height */
overflow: hidden; /* 隐藏溢出内容 */
transition: max-height 0.5s ease-out; /* 使用transition,animation原理类似 */
}
.expand-box.is-expanded {
max-height: 500px; /* 展开到足够大的值 */
}
/* 如果用animation,@keyframes可以这样定义 */
@keyframes expandContent {
from {
max-height: 0;
}
to {
max-height: 500px; /* 假设最大高度不会超过500px */
}
}
.animated-expand-box {
height: auto; /* 实际高度由内容决定 */
max-height: 0;
overflow: hidden;
animation-name: expandContent;
animation-duration: 0.5s;
animation-fill-mode: forwards;
/* ...其他动画属性 */
}再者,box-sizing
width
height
padding
border
content-box
border-box
border-box
width
height
padding
border
content-box
width
height
最后,动画的缓动函数(timing-function)同样重要。
ease-in-out
linear
cubic-bezier
linear
cubic-bezier
正如我前面提到的,直接动画
height
width
一个非常有效的策略是优先使用transform: scale()
scale()
transform
@keyframes scaleChange {
0% {
transform: scale(1); /* 初始比例 */
background-color: lightblue;
}
50% {
transform: scale(1.5); /* 放大1.5倍 */
background-color: coral;
}
100% {
transform: scale(1.2); /* 缩小到1.2倍 */
background-color: lightgreen;
}
}
.animated-scaled-box {
width: 100px; /* 元素实际占据的空间不变 */
height: 50px;
background-color: lightblue;
border: 1px solid #333;
margin: 20px;
animation-name: scaleChange;
animation-duration: 4s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
animation-direction: alternate;
}你看,这个例子中,
width
height
transform: scale()
当确实需要改变元素在文档流中占据的空间时,例如手风琴效果的展开,max-height
max-width
overflow: hidden
height
width
auto
max-height
0
auto
height
max-height
此外,合理使用will-change
will-change
.potentially-animated-element {
/* ...其他样式 */
will-change: width, height, transform; /* 告诉浏览器这些属性可能会变化 */
}最后,简化keyframes
from
to
animation
animation
1. 优雅的展开/折叠组件(Accordion/Collapse):这是最经典的用例之一。当我们点击一个标题,下面的内容区域平滑地展开或折叠,这通常就是通过
animation
transition
max-height
/* 一个简单的手风琴展开动画 */
@keyframes accordionExpand {
from {
max-height: 0;
opacity: 0;
}
to {
max-height: 300px; /* 假设内容最大高度 */
opacity: 1;
}
}
.accordion-content {
overflow: hidden;
max-height: 0;
opacity: 0;
/* 当某个类被添加时触发动画 */
/* .is-expanded {
animation: accordionExpand 0.5s ease-out forwards;
} */
}2. 模态框(Modal)和侧边栏(Sidebar)的登场与退场:这些覆盖层组件的出现和消失,如果只是生硬地显示隐藏,体验会很差。通过
animation
transform
opacity
width: 0
width: 300px
3. 骨架屏(Skeleton Loaders)的动态占位:在内容加载完成之前,我们通常会显示一个灰色的占位符,这就是骨架屏。骨架屏中的文本行、图片区域等,它们的尺寸和形状是预设的。
animation
gradient
4. 动态图表和数据可视化:这绝对是
animation
animation
5. 微交互(Micro-interactions)中的反馈:用户点击按钮、输入表单时,我们希望给他们即时反馈。一个按钮在点击后,可以有一个微小的宽度或高度收缩动画,然后恢复,模拟“按下”的物理感。或者一个输入框在获得焦点时,底部边框可以有一个从细到粗的宽度动画。这些细微的尺寸变化,虽然不影响布局,却极大地提升了用户界面的“活泼感”。
这些创新应用的核心思想都是一样的:利用CSS
animation
以上就是如何通过css animation控制高度和宽度变化的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号