当前位置:Gxlcms > css > css实现圆与边框旋转动画的代码实例

css实现圆与边框旋转动画的代码实例

时间:2021-07-01 10:21:17 帮助过:76人阅读

本篇文章给大家带来的内容是关于css实现圆与边框的代码实例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

实现效果:

524676735-5b9fcb7ccb79d_articlex.png

代码

html:

  1. <div id="box">
  2. <div class="circle-out">
  3. <div class="circle-inner"> </div>
  4. </div>
  5. <div class="circle-part">
  6. </div>
  7. <div class="part1">
  8. </div>
  9. </div>

css:

  1. #box {
  2. height:200px;
  3. width:200px;
  4. }
  5. .circle-out{
  6. height: inherit;
  7. width: inherit;
  8. display: inline-block;
  9. text-align: center;
  10. border: 20px solid blue;
  11. border-radius: 50%;
  12. }
  13. /* 绘制弧形 */
  14. .circle-part{
  15. display: inline-block;
  16. position: relative;
  17. width:0px;
  18. height: 0px;
  19. border-radius: 50%;
  20. border: 100px solid #0000ff05;
  21. border-top: 100px solid blue;
  22. top: -220px;
  23. left: 20px;
  24. transform: rotate(0deg);
  25. animation: run-part 5s infinite;
  26. }
  27. .part1{
  28. height: 0px;
  29. width: 0px;
  30. border-radius: 50%;
  31. border:100px solid #fafafa;
  32. border-top: 100px solid #ff000000;
  33. position: relative;
  34. top: -420px;
  35. left: 20px;
  36. transform: rotate(45deg);
  37. animation: run-part1 5s infinite;
  38. }
  39. .circle-inner{
  40. height: 0px;
  41. width: 0px;
  42. display: inline-block;
  43. border-radius: 50%;
  44. border: 20px solid blue;
  45. top: 80px;
  46. position: relative;
  47. z-index: 1000;
  48. }
  49. @-webkit-keyframes run-part1{
  50. 0%{
  51. transform: rotate(45deg);
  52. }
  53. 100% {
  54. transform: rotate(405deg);
  55. }
  56. }
  57. @-webkit-keyframes run-part{
  58. 0%{
  59. transform: rotate(0deg);
  60. }
  61. 100% {
  62. transform: rotate(360deg);
  63. }
  64. }

实现思路

1 图形构成

从外观看到,该图形大致由:外圆,内圆及构扇形构成。

1.1 外圆

在本示例中,主要采用一个p,设置高与宽,背景不设置或白色。设置 border-radius为50%外圆圈,使用边框构成从而形成外圈。

  1. .circle-out{
  2. height: inherit;
  3. width: inherit;
  4. border: 20px solid blue;
  5. display: inline-block;
  6. border-radius: 50%;
  7. text-align: center;
  8. }

效果图

3291946171-5b9fcb7d3ff68_articlex.png

1.2内圆

内圆很简单,也是使用border完成的圆,设置boder-radius:50%实现的圆的效果,最后就是一个定位的事情。

1.3扇形

扇形,在本示例中,实现的思路也是拼凑,外加旋转,利用边框border实现。

  1. .circle-part{
  2. //(1)
  3. display: inline-block;
  4. width:0px;
  5. height: 0px;
  6. //(2)
  7. border-radius: 50%;
  8. border: 100px solid #0000ff05;
  9. border-top: 100px solid blue;
  10. //(3)
  11. position: relative;
  12. top: -220px;
  13. left: 20px;
  14. //(4)
  15. transform: rotate(0deg);
  16. animation: run-part 5s infinite;
  17. }

如上代码:
分为(1)、(2)、(3)、(4)部分,出去固定形状、动画外,比较重要的就在于(2)部分。

先绘制出1/4的圆(边框)。其他另外3/4的扇形以透明绘制。

相同的,另外使用另外一个圆进行相同的处理,这样两个圆就能重叠在一起,唯一不同的是:第二个圆设置那3/4圆作为白色,1/4设置为透明色。

这时,呈现的为1/4的扇形,背景为blue,而因为透明的原因1/4是完全暴露的。

最后,由于最后的圆为顶层元素,所以当顶层元素发生旋转时,蓝色的扇形部分就会被顶层元素那3/4的扇形区域所遮蔽。从而达到最后的效果。

代码最后加上自己的动画,实现最后的效果。

以上就是css实现圆与边框旋转动画的代码实例的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行