html - 带有 <style> 标签的 SVG 动画问题

标签 html css svg

我正在尝试在 HTML 中添加 CSS 代码,但我无法让 SVG 动画正常工作。我在这里做错了什么?我的客人是我正在引用 <style>错误,但经过几次尝试后我无法让它工作。

<svg>
  <style type="text/css">
    .spinner {
      animation: rotate 2s linear infinite;
      z-index: 2;
      position: absolute;
      top: 50%;
      left: 50%;
      margin: -25px 0 0 -25px;
      width: 50px;
      height: 50px;
    }

    .path {
      stroke: hsl(210, 70, 75);
      stroke-linecap: round;
      animation: dash 1.5s ease-in-out infinite;
    }

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

      @keyframes dash {
        0% {
          stroke-dasharray: 1, 150;
          stroke-dashoffset: 0;
        }
        50% {
          stroke-dasharray: 90, 150;
          stroke-dashoffset: -35;
        }
        100% {
          stroke-dasharray: 90, 150;
          stroke-dashoffset: -124;
        }
      }
  </style>
  <svg class="spinner">
      <circle class="path" cx="25" cy="25" r="20" fill="none" stroke-width="5"</circle>
  </svg>
</svg>

CodePen

提前谢谢你。

最佳答案

您的动画有效。您弄乱了描边颜色定义。 hsl(210, 70%, 75%); 和圆圈元素开始标记缺少结束括号 >

<circle class="path" cx="25" cy="25" r="20" fill="none" stroke-width="5" > </circle>

html, body {
  height: 100%;
  background-color: #000000;
}
<svg>
  <style type="text/css">
    .spinner {
      animation: rotate 2s linear infinite;
      transform-origin:center center;
      z-index: 2;
      position: absolute;
      top: 50%;
      left: 50%;
      margin: -25px 0 0 -25px;
      width: 50px;
      height: 50px;
    }
  
    .path {
      stroke: hsl(210, 70%, 75%);
      stroke-linecap: round;
      animation: dash 1.5s ease-in-out infinite;
    }
  
      @keyframes rotate {
        0% {
          transform: rotate(0deg);
        }
        100% {
          transform: rotate(359deg);
        }
      }
  
      @keyframes dash {
        0% {
          stroke-dasharray: 1, 150;
          stroke-dashoffset: 0;
        }
        50% {
          stroke-dasharray: 90, 150;
          stroke-dashoffset: -35;
        }
        100% {
          stroke-dasharray: 90, 150;
          stroke-dashoffset: -124;
        }
      }
  </style>
  <g class="spinner">
      <circle class="path" cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
  </g>
</svg>

关于html - 带有 &lt;style&gt; 标签的 SVG 动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42782303/

相关文章:

javascript - 如何使用图片作为提交按钮触发php函数

html - 在 HTML 中显示僧伽罗语字母

html - Google Chrome 不会在 HTML 网页中将单词加粗

html - 如何并排设置2张图片?

javascript - 为什么 SVG 的 getElementById 失败?

javascript - 更改在发生过程中创建框的速度(与 DOM "change"事件相关)

javascript - 原型(prototype)js使表可调整列大小

php - 使用 PHP 将类添加到 MySQL 数据的 HTML 表的单元格

javascript - 在 meteor 中导入 svg 文件

javascript - SVG 旋转变换矩阵