首页 > web前端 > css教程 > 正文

如何仅使用 CSS 创建具有透明背景的平滑动画圆形绘制效果?

Mary-Kate Olsen
发布: 2024-10-25 05:44:29
原创
827 人浏览过

How to Create a Smoothly Animated Circle Drawing Effect with a Transparent Background Using CSS Only?

仅 CSS 动画绘制带有边框半径和透明背景的圆

问题:

如何创建动画边框-半径为透明背景的圆,同时遮盖圆的初始部分以获得绘图效果?

解决方案:

  1. 建立画布:

    • 使用绝对定位和边框定义容器以供参考。
    • 为遮罩创建一个 50% 宽度、绝对定位的半剪辑元素。
  2. 创建圆圈:

    • 在半剪辑内,放置一个具有透明边框和蓝色顶部的圆圈,然后左边框颜色。
    • 将圆圈在半剪辑内右对齐。
  3. 动画绘图:

    • 使用 CSS 动画将圆从初始 -45 度位置旋转 135 度,以呈现绘图的外观。
    • 将半剪辑动画旋转 360 度以保持遮罩不变
  4. 保持透明度:

    • 向主体添加重复的线性渐变,以确保透明度和提供视觉上下文。
    • 将渐变缩放到容器大小并将背景重复设置为不重复。
  5. 修复半圆:

    • 创建第二个半圆(固定),无动画,旋转 135 度。
    • 动画固定半圆的不透明度,使其在 50% 后出现
  6. 控制动画时间:

    • 根据需要调整动画持续时间和时间以实现所需的绘制速度和效果。

代码片段:

<code class="css">body {
  background: repeating-linear-gradient(45deg, white 0px, lightblue 100px);
  height: 500px;
  background-size: 500px 500px;
  background-repeat: no-repeat;
}

#container {
  position: absolute;
  width: 400px;
  height: 400px;
  border: solid red 1px;
  animation: colors 4s infinite;
}

#halfclip {
  width: 50%;
  height: 100%;
  right: 0px;
  position: absolute;
  overflow: hidden;
  transform-origin: left center;
  animation: cliprotate 16s steps(2) infinite;
  -webkit-animation: cliprotate 16s steps(2) infinite;
}

.halfcircle {
  box-sizing: border-box;
  height: 100%;
  right: 0px;
  position: absolute;
  border: solid 25px transparent;
  border-top-color: blue;
  border-left-color: blue;
  border-radius: 50%;
}

#clipped {
  width: 200%;
  animation: rotate 8s linear infinite;
  -webkit-animation: rotate 8s linear infinite;
}

#fixed {
  width: 100%;
  transform: rotate(135deg);
  animation: showfixed 16s steps(2) infinite;
  -webkit-animation: showfixed 16s linear infinite;
}

@-webkit-keyframes cliprotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

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

@-webkit-keyframes rotate {
  0% { transform: rotate(-45deg); }
  100% { transform: rotate(135deg); }
}

@keyframes rotate {
  0% { transform: rotate(-45deg); }
  100% { transform: rotate(135deg); }
}

@-webkit-keyframes showfixed {
  0% { opacity: 0; }
  49.9% { opacity: 0; }
  50% { opacity: 1; }
  100% { opacity: 1; }
}
</code>
登录后复制

以上是如何仅使用 CSS 创建具有透明背景的平滑动画圆形绘制效果?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!