CSS100Day(21)-利用svg线段实现吃小球动画
561 字
3 分钟
CSS100Day(21)-利用svg线段实现吃小球动画
设计简介
关键点如下
-
知识点1:其实svg也可以做实心圆,例如例子中的人,张嘴使用
stroke-dashoffset和rotate:45deg实现 -
知识点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; }}分享给其他人
如果该文章对您有帮助,欢迎分享给其他人阅读!
复制链接
相关文章
推荐筛选
1
CSS100Day(10)-SVG旋转动画
设计灵感 巧妙应用SVG的
2
CSS100Day(17)-svg实现彭罗斯三角
设计灵感 三个svg实现彭罗斯三角
3
CSS100Day(16)-利用before、after伪元素完成长方形散花动画
设计灵感 伪元素实现长方形散花动画
4
CSS100Day(3)-clip-path和动画实现金字塔日出日落
设计灵感 一个训练clip-path和关键帧贝塞尔曲线的练习
5
CSS100Day(18)-3D实现弹性元素
设计灵感 3D实现弹性元素
随机文章
随机推荐