CSS100Day(21)-利用svg线段实现吃小球动画

561 字
3 分钟
CSS100Day(21)-利用svg线段实现吃小球动画

设计简介#

关键点如下

  1. 知识点1:其实svg也可以做实心圆,例如例子中的人,张嘴使用 stroke-dashoffsetrotate:45deg实现

  2. 知识点2:小球svg比较难,首先设置实线 stroke-dasharray:0px 50px让其0px,间隔50px,所以看不到任何东西,但使用 stroke-linecap: round 该属性表示间隔实线的终点为圆,其大小为 stroke-width

效果展示#

实现代码和步骤#

HTML部分#

<div class="frame">
<svg class="pacman">
<circle cx="50" cy="50" r="25" />
</svg>
<svg class="eye">
<circle cx="6" cy="6" r="5" />
</svg>
<svg class="dots">
<polyline points="0,7 240,7" />
</svg>
</div>

CSS部分#

:root {
--radius: 50;
}
.frame {
position: relative;
box-shadow: 1px 2px 10px 0 rgba(0, 0, 0, 0.3);
border-radius: 2px;
width: 400px;
height: 400px;
overflow: hidden;
background: #2c2e2e;
}
.pacman {
position: absolute;
width: 100px;
height: 100px;
top: 150px;
left: 150px;
fill: none;
stroke: #f4d146;
stroke-width: 50px; /* 线段宽度就是圆半径 */
stroke-dasharray: calc(50 * 3.14px); /* 间隔就是整个周长,后续可以实现张嘴 */
animation: mouth 0.7s ease-in-out infinite;
}
.eye {
position: absolute;
fill: #2c2e2e;
width: 12px;
height: 12px;
top: 167px;
left: 204px;
animation: eye 0.7s ease-in-out infinite;
}
.dots {
position: absolute;
height: 14px;
width: 240px;
top: 193px;
left: 180px;
stroke: #f4d146;
stroke-width: 14px; /* 实线线帽的半径 */
stroke-dasharray: 0px 50px; /* 实线为0,所以看不到 */
stroke-dashoffset: 14;
stroke-linecap: round; /* 每个实线的线帽子为圆,这样就可以显示间隔显示圆 */
animation: points 0.7s linear infinite;
}
@keyframes mouth {
0%,
100% {
stroke-dashoffset: 0;
rotate: 0deg;
}
50% {
stroke-dashoffset: calc(50 * 3.14px / 4);
rotate: 45deg;
}
}
@keyframes eye {
0%,
100% {
translate: 0 0;
}
50% {
translate: -6px -3px;
}
}
@keyframes points {
0% {
stroke-dashoffset: 14;
transform: translate3d(0, 0, 0);
}
100% {
stroke-dashoffset: 64;
}
}
分享给其他人
如果该文章对您有帮助,欢迎分享给其他人阅读!
复制链接

评论区

图片
犟哟
Hello, I'm Juyial.
公告
欢迎来到我的博客,在这里可以一起进步,一起学习!2026年起航!
分类
站点统计
文章
19
分类
1
标签
3
总字数
35,134
运行时长
0
最后活动
0 天前

目录