css - 中心加载动画

css - center loading animation

我想在按钮中间添加一个旋转的动画,像这样:

.button {
  position: absolute;
  display: inline-block;
  border: 1px solid grey;
  padding: 20px;
}

.button::after {
    content: "";
    position: absolute;
    border: 8px solid #ddd;
    border-radius: 50%;
    border-top: 8px solid #008;
    border-bottom: 8px solid #008;
    width: 64px;
    height: 64px;
    animation: spin 2s linear infinite;  /* without the animation, everything is positioned perfectly */
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

动画看起来符合预期,但元素未居中。当 "animation" 属性 被移除时,元素被正确定位。如何将加载动画定位到按钮的中心?

https://jsfiddle.net/f9993ttm/

您的动画正在覆盖您的变换 属性。

尝试更像:

@keyframes spin {
    0% { transform: translate(-50%, -50%) rotate(0deg) }
    100% { transform: translate(-50%, -50%) rotate(360deg)  }
}