制作css加载进度条动画的核心是利用@keyframes定义样式变化,并通过animation属性应用;要让动画更平滑,1. 使用ease-in-out或自定义cubic-bezier时间函数优化速度曲线,2. 优先使用transform(如translatex或scalex)替代width变化以提升性能,3. 设置animation-fill-mode: forwards确保动画结束状态停留;对于不确定性加载动画,可1. 利用background-image配合background-position动画实现流动渐变效果,2. 使用伪元素结合transform和opacity创建光斑扫过动画;控制加载状态需结合javascript,1. 通过添加或移除类名控制不同状态(如is-loading、is-complete),2. 使用animation-play-state: paused/running实现动画暂停与恢复,3. 用opacity过渡替代display切换以实现淡入淡出效果,从而提升用户体验,最终达到缓解等待焦虑的目的。

CSS制作加载进度条动画,核心在于利用
@keyframes
让进度条动画看起来更“活”一点,这其实是个美学与技术结合的问题。我个人在实践中发现,关键在于动画的时间函数(
animation-timing-function
首先,
animation-timing-function
ease
ease-in-out
cubic-bezier
cubic-bezier(0.68, -0.55, 0.27, 1.55)
立即学习“前端免费学习笔记(深入)”;
其次,动画属性的选择也影响很大。我通常建议优先使用
transform
transform: translateX()
scaleX()
width
transform
width
还有个小细节,就是
animation-fill-mode: forwards;
不确定性加载动画,或者说“骨架屏”那种效果,是当你不清楚具体加载进度时非常实用的方案。它更多是给用户一个“系统正在忙碌”的视觉反馈,而不是具体的百分比。这里有几种我常用的方法:
一种常见的做法是利用
background-image
background-position
linear-gradient
@keyframes
background-position
.indeterminate-progress {
width: 100%;
height: 8px;
background-color: #e0e0e0;
border-radius: 4px;
overflow: hidden;
position: relative;
}
.indeterminate-progress::before {
content: '';
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: linear-gradient(to right, transparent 0%, #4CAF50 50%, transparent 100%);
background-size: 200% 100%; /* 确保渐变条纹足够长 */
animation: indeterminateMove 1.5s linear infinite;
}
@keyframes indeterminateMove {
0% {
background-position: -100% 0; /* 从左边完全移出 */
}
100% {
background-position: 100% 0; /* 移到右边完全移出 */
}
}另一种方法是利用伪元素(
::before
::after
transform
opacity
box-shadow
控制进度条的显示和状态,这通常需要JavaScript的介入,但CSS本身也提供了一些很方便的属性来配合。
最直接的方式是利用CSS类(class)来控制。当加载开始时,通过JavaScript给进度条元素添加一个特定的类(比如
is-loading
is-complete
// 假设你有一个进度条元素
const progressBar = document.querySelector('.progress-bar');
// 加载开始时
function startLoading() {
progressBar.style.width = '0%'; // 重置进度
progressBar.classList.add('is-loading');
progressBar.classList.remove('is-complete');
}
// 加载进行中(如果是确定性进度条)
function updateProgress(percentage) {
progressBar.style.width = `${percentage}%`;
}
// 加载完成时
function finishLoading() {
progressBar.style.width = '100%'; // 确保最终是100%
progressBar.classList.remove('is-loading');
progressBar.classList.add('is-complete');
// 动画结束后可以隐藏
setTimeout(() => {
// progressBar.style.display = 'none'; // 或者通过opacity逐渐隐藏
}, 500); // 等待动画完成
}
// 暂停动画
// CSS: animation-play-state: paused;
function pauseAnimation() {
progressBar.style.animationPlayState = 'paused';
}
// 继续动画
// CSS: animation-play-state: running;
function resumeAnimation() {
progressBar.style.animationPlayState = 'running';
}在CSS中,
animation-play-state
running
paused
至于显示与隐藏,除了直接改变
display
opacity
transition
opacity
总的来说,CSS动画在进度条上的应用,是前端体验优化里一个不容忽视的细节。它能有效缓解用户的等待焦虑,哪怕只是一个简单的视觉反馈,也能让用户感觉流程更顺畅。
以上就是CSS怎样制作加载进度条动画?@keyframes关键帧控制的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号