为CSS实现的胡子元素添加摇晃动画不生效,求排查修复方案
胡子动画失效问题修复
核心问题定位
- 动画循环参数拼写错误:代码中所有用到的
infinte为错误拼写,浏览器无法识别该参数值,会直接丢弃对应的animation规则,导致动画完全无法触发。正确拼写为infinite。 - 右侧胡子的动画关键帧逻辑错误:当前
@keyframes shakeRight的旋转值全部为负角度,和左侧胡子的动画参数完全一致。但右侧伪元素的基础形态是rotate(40deg),动画启动后会直接被拽到左侧位置,破坏整体造型。 - 冗余重复样式:分别用双冒号伪类选择器、单冒号伪类选择器重复写了两次完全相同的动画配置,没有实际作用,反而提升排查成本。
修正后可直接运行的代码
.mustache { width: 180px; height: 180px; left: 100px; border-radius: 50%; position: absolute; color: pink; box-shadow: 150px 240px 0 0 currentColor, 300px 240px 0 0 currentColor; } .mustache::before { content: ""; position: absolute; left: 30px; top: 120px; width: 210px; height: 120px; border-bottom: solid 180px currentColor; border-radius: 0 0 0 100%; transform: rotate(-40deg); transform-origin: right 210px; animation: shakeLeft 1s ease-in-out infinite; } .mustache::after { content: ""; position: absolute; left: 390px; top: 120px; width: 210px; height: 120px; border-bottom: solid 180px currentColor; border-radius: 0 0 100% 0; transform: rotate(40deg); transform-origin: left 210px; animation: shakeRight 1s ease-in-out infinite; } @keyframes shakeLeft { 0% { transform: rotate(-50deg); } 50% { transform: rotate(-30deg); } 100% { transform: rotate(-50deg); } } @keyframes shakeRight { 0% { transform: rotate(50deg); } 50% { transform: rotate(30deg); } 100% { transform: rotate(50deg); } }
视觉优化建议:如果觉得当前20度的摇晃幅度过大,可以将两侧关键帧的角度调整为±45度、±35度,让摇晃动作更自然,不会因为幅度过大破坏胡子的整体造型。
内容的提问来源于stack exchange,提问作者Daisy
相关产品推荐
相关产品推荐

