简单CSS关键帧圆形动画运行异常,多次排查仍未定位错误求助
导致动画不生效的核心原因
- 关键帧命名非法:CSS规范要求自定义标识符(包括@keyframes的规则名)不能以数字开头,当前代码中
@keyframes 123、.circles类里设置的animation-name: 123属于语法无效内容,浏览器无法完成关键帧匹配,整个动画规则不会被触发。 - 动画关键帧定义不完整:当前仅设置了动画进度50%位置的
bottom: 10%属性,缺失起始(0%)、循环复位(100%)位置的垂直定位规则;同时固定定位的圆形元素没有设置初始垂直坐标,元素初始位置会沿用文档流的默认排布,无法实现流畅的上下位移动画。 - HTML结构错位:引入Lobster字体的link标签被放置在
<html>根标签与<head>标签的嵌套空隙中,不符合HTML文档的结构规范,属于依赖浏览器容错的非标准写法。
可直接运行的修正后代码
body { background: hsl(210, 100%, 95%); } #Title { font-family: Lobster, monospace; font-size: 45px; text-align: center; color: white; } h1:hover { color: black; transform: scale(1.2) } .circles { height: 80px; width: 80px; border-radius: 50%; border: solid; position: fixed; /* 匹配合法的关键帧名称 */ animation-name: circleFloat; animation-iteration-count: infinite; animation-duration: 10s; /* 补全固定定位初始垂直值,避免初始位置偏移 */ bottom: 85%; } #circle1 { animation-timing-function: linear; background: linear-gradient(45deg, #ccffff, #ffcccc); left: 50%; } #circle2 { animation-timing-function: linear; background: repeating-linear-gradient(90deg, #A52A2A 2px, #cccccc 5px, orange 10px); left: 25%; } /* 替换非法的数字开头关键帧名,补全完整的动画关键帧状态 */ @keyframes circleFloat { 0% { bottom: 85%; } 50% { bottom: 10%; } 100% { bottom: 85%; } }
<!DOCTYPE html> <html> <head> <title>Animation-1</title> <!-- 将link标签移入head标签内部,修正结构错位问题 --> <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css"> </head> <body> <h1 id="Title">Placeholder</h1> <div class="circles" id="circle1"></div> <div class="circles" id="circle2"></div> </body> </html>
内容的提问来源于stack exchange,提问作者Bassam Addas
相关产品推荐
相关产品推荐

