css - SVG <animate> 从 0 的偏移量不起作用

标签 css svg offset linear-gradients svg-animate

动画的目的是填充背景颜色作为加载电池。在我看来,解决方案似乎是苹果渐变动画,显示为正在加载。但在当前的 SVG 中,当属性 offset="0%"

时, LinearGradient 动画不起作用

div {
  float: left;
  width: 50%;
  padding: 10px;
  box-sizing: border-box;
}
<div>
  <p><small>offset from 0%</small> <strong>not working</strong></p>
  <svg viewBox="0 0 110 88" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="mysvg">
    <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
      <g id="Group" fill="#EAEAEA">
        <rect id="Brick-4" x="77.5" y="10" width="13.5" height="68" rx="3"></rect>
        <rect id="Brick-3" x="56" y="10" width="13.5" height="68" rx="3"></rect>
        <rect id="Brick-2" x="34.5" y="10" width="13.5" height="68" rx="3"></rect>
        <rect id="Brick-1" x="13" y="10" width="13.5" height="68" rx="3"></rect>
        <path d="M104,30.2724875 C104.164709,30.2576038 104.331485,30.25 104.5,30.25 C107.537566,30.25 110,32.7220822 110,35.7458573 L110,53.1708094 C110,56.2060875 107.531385,58.6666667 104.5,58.6666667 C104.331455,58.6666667 104.164681,58.6590557 104,58.6441617 L104,82.0033234 C104,85.3151964 101.311488,88 97.9970362,88 L6.0029638,88 C2.68761844,88 0,85.3182852 0,82.0033234 L0,5.99667663 C0,2.68480358 2.68851188,0 6.0029638,0 L97.9970362,0 C101.312382,0 104,2.68171476 104,5.99667663 L104,30.2724875 L104,30.2724875 Z M5,10.991014 C5,7.68226832 7.68678744,5 11.0051618,5 L92.9948382,5 C96.3113975,5 99,7.68493655 99,10.991014 L99,77.008986 C99,80.3177317 96.3132126,83 92.9948382,83 L11.0051618,83 C7.68860254,83 5,80.3150634 5,77.008986 L5,10.991014 Z"
        fill="url(#fromzero)
        "></path>
        <defs>
          <linearGradient id="fromzero" x1="0">
            <stop offset="0%" stop-color="#4c7eaf"></stop>
            <stop offset="0%" stop-color="#828282"></stop>
            <animate attributeName="x1" dur="5s" from="0%" to="100%" repeatCount="indefinite" />
          </linearGradient>
        </defs>
      </g>
    </g>
  </svg>
</div>
<div>
  <p><small>offset from 1%</small> <strong>working</strong></p>
  <svg viewBox="0 0 110 88" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="mysvg">
    <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
      <g id="Group" fill="#EAEAEA">
        <rect id="Brick-4" x="77.5" y="10" width="13.5" height="68" rx="3"></rect>
        <rect id="Brick-3" x="56" y="10" width="13.5" height="68" rx="3"></rect>
        <rect id="Brick-2" x="34.5" y="10" width="13.5" height="68" rx="3"></rect>
        <rect id="Brick-1" x="13" y="10" width="13.5" height="68" rx="3"></rect>
        <path d="M104,30.2724875 C104.164709,30.2576038 104.331485,30.25 104.5,30.25 C107.537566,30.25 110,32.7220822 110,35.7458573 L110,53.1708094 C110,56.2060875 107.531385,58.6666667 104.5,58.6666667 C104.331455,58.6666667 104.164681,58.6590557 104,58.6441617 L104,82.0033234 C104,85.3151964 101.311488,88 97.9970362,88 L6.0029638,88 C2.68761844,88 0,85.3182852 0,82.0033234 L0,5.99667663 C0,2.68480358 2.68851188,0 6.0029638,0 L97.9970362,0 C101.312382,0 104,2.68171476 104,5.99667663 L104,30.2724875 L104,30.2724875 Z M5,10.991014 C5,7.68226832 7.68678744,5 11.0051618,5 L92.9948382,5 C96.3113975,5 99,7.68493655 99,10.991014 L99,77.008986 C99,80.3177317 96.3132126,83 92.9948382,83 L11.0051618,83 C7.68860254,83 5,80.3150634 5,77.008986 L5,10.991014 Z"
        fill="url(#fromone)
        "></path>
        <defs>
          <linearGradient id="fromone" x1="0">
            <stop offset="1%" stop-color="#4c7eaf"></stop>
            <stop offset="1%" stop-color="#828282"></stop>
            <animate attributeName="x1" dur="5s" from="0%" to="100%" repeatCount="indefinite" />
          </linearGradient>
        </defs>
      </g>
    </g>
  </svg>
</div>

最佳答案

将代码更改为这样

<linearGradient id="fromzero" x1="0">
    <stop offset="50%" stop-color="#4c7eaf"></stop>
    <stop offset="50%" stop-color="#828282"></stop>
    <animate attributeName="x1" dur="5s" from="-100%" to="100%" repeatCount="indefinite" />
</linearGradient>

使渐变将背景颜色分割为两种颜色,并将其从 -100% 移动到 100%,从而可以从 0% 到 100%,同时给出完全相同的效果。

我现在还添加了您想要的动画。它是通过点击 svg 元素触发的。

这是一个Fiddle结果

希望这有帮助

关于css - SVG <animate> 从 0 的偏移量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33231088/

相关文章:

html - SVG 矩形与 div 与 Canvas

SVG 水平大括号

javascript - 更新 firefox 55 后,内联图像 (img) 未通过 SVG 在 HTML CANVAS 中呈现

jquery - jQuery的scrollTo问题

html - CSS:垂直对齐文本?

javascript - 带有上一个下一个部分图像的全宽轮播 slider

jquery - 如何仅将效果应用于悬停的元素?

sql - 确定记录在哪一页上

具有动态行参数的 Excel OFFSET 函数

javascript - 尝试从多个输入中获取值