Animate svg path fill using css

2019-06-19 00:36发布

It is possible to change svg fill using css. But I didn't manage to create an animation. Here is my svg object:

<svg version="1.1" id="tick" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100%" height="100%" viewBox="0 0 60 60" enable-background="new 0 0 60 60" xml:space="preserve"> 
<rect id="fill" x="21" y="26" width="60" height="60"/>
</svg>

And, using SASS:

@include keyframes(loading)
  0%
    fill: black
  50%
    fill: white
  100%
    fill: black
#fill
  fill: white
  @include animation(loading 3s infinite)

What am I doing wrong?

1条回答
我命由我不由天
2楼-- · 2019-06-19 00:47

EDIT

Okay it looks like something like this should work.

    <svg version="1.1" id="tick" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100%" height="100%" viewBox="0 0 60 60" enable-background="new 0 0 60 60" xml:space="preserve"> 
  <style type="text/css" >
        <![CDATA[

          @keyframes fill {
              0% {
                  fill: black;
              }
              50% {
                  fill: white;
              }
              100% {
                  fill: black;
              }
          }

          #fill {
              fill: black;
              animation-name: fill;
              animation-duration: 3s;
              animation-iteration-count: infinite;
          }

        ]]>
    </style>
    <rect id="fill" x="21" y="26" width="60" height="60"/>
</svg>

Here is the example: http://jsbin.com/ayiheb/2/edit

查看更多
登录 后发表回答