加载旋转图标(或活动指示器)会在确切时长未知时告知用户某个流程正在进行中。与进度环不同,旋转图标是“不确定”的,通常使用动画来表示活动。
本指南通过以下方式实现微调框:
- 使用原生
<progress>元素作为语义基础。通过省略value属性,浏览器会将其视为不确定进度条,从而确保辅助技术正确读出。 - 使用
conic-gradient()为组件设置样式,以创建视觉“轨迹”,并使用background-clip: border-area将中心挖空成环形。 - 使用 CSS 转换高效地为旋转图标添加动画效果,并遵循
prefers-reduced-motion,确保为所有用户提供舒适的体验。
如需处理时长已知的确定性任务,请参阅进度环。
实现
1. Markup
将原生 <progress> 元素同时用作语义可信来源和视觉组件。如果没有 value 属性,则隐式不确定。
<progress aria-label="Loading" class="loading-spinner"></progress>
2. 样式
隐藏原生界面
如需将 <progress> 元素设置为旋转图标样式,请先隐藏不确定进度条的默认浏览器样式。
/* Hide native bars */
progress.loading-spinner:indeterminate::-webkit-progress-bar {
display: none;
background: none;
}
progress.loading-spinner:indeterminate::-webkit-progress-value {
display: none;
background: none;
}
progress.loading-spinner:indeterminate::-moz-progress-bar {
display: none;
background: none;
}
progress.loading-spinner:indeterminate::slider-fill {
display: none;
background: none;
}
旋转环和轨迹
旋转图标使用 conic-gradient 创建视觉轨迹。
@property --arc-start {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
@property --arc-end {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
/* Use a custom property to conditionally include the dash animation */
@property --progress-dash-animation {
syntax: "*";
inherits: false;
initial-value: , progress-dash 3s ease-in-out infinite;
}
progress.loading-spinner:indeterminate {
--_from: calc(90deg + var(--arc-start, 0deg));
--_to: calc(90deg + var(--arc-end, 158deg));
--size: 40px;
--thickness: 2px;
--spinner-color: #3b82f6;
--track-color: #e2e5e7;
--spinner-duration: 1.5s;
--_used-spinner-duration: var(--spinner-duration);
--spinner-timing: linear;
position: relative;
width: var(--size);
height: var(--size);
border-radius: 50%;
appearance: none;
/* Create the fading trail with dynamic angles */
background: conic-gradient(
from var(--_from),
var(--spinner-color) calc(var(--_to) - var(--_from)),
transparent 0
)
var(--track-color);
@supports (background-clip: border-area) {
background-clip: border-area;
border: var(--thickness) solid transparent;
background-origin: border-box;
}
/* ... fallback for background-clip: border-area ... */
/* The dash animation is only included if @property is supported */
animation:
progress-spin var(--_used-spinner-duration) linear infinite
var(--progress-dash-animation, );
}
@keyframes progress-spin {
to {
rotate: 1turn;
}
}
@keyframes progress-dash {
from {
--arc-start: 0deg;
--arc-end: 3deg;
}
50% {
--arc-start: 100deg;
--arc-end: 358deg;
}
to {
--arc-start: 360deg;
--arc-end: 363deg;
}
}
遵循动作偏好设置
对运动敏感的用户可能会觉得快速旋转的元素令人眩晕。请始终遵循 prefers-reduced-motion 媒体查询。设置内部 --_used-spinner-duration 属性以覆盖用户的 --spinner-duration 值。
@media (prefers-reduced-motion: reduce) {
.loading-spinner {
/* Slow down the animation significantly rather than stopping it entirely,
so the user still knows that the process is active. */
--_used-spinner-duration: 6s;
}
}
后备策略
如果支持 @property,则系统会通过 --progress-dash-animation 属性的 initial-value 自动添加虚线动画。在不支持 @property 的浏览器中,系统会忽略属性注册,动画会回退到简单的旋转。此后备方案不需要 JavaScript。
对于尚不支持 background-clip: border-area 的浏览器,请回退到 mask-image 以将中心挖空。
/* Fallback: use mask-image to create the ring */
@supports not (background-clip: border-area) {
--clip-boundary: calc(100% - var(--thickness));
mask-image: radial-gradient(
farthest-side,
transparent var(--clip-boundary),
black var(--clip-boundary)
);
border: 0;
}