CSS如何从当前帧恢复鼠标输出上的SVG动画

时间:2015-04-27 04:33:29

标签: css animation svg

我正在使用SVG进行一些按钮动画,并且无法使其正常工作。我试过找同样的情况,但没有运气。所以我最终到了这里,因为我已经花了太多时间在这上面。任何帮助将非常感激。 以下是代码:http://jsfiddle.net/wq4djg9z/2/ 它工作正常,但有一个缺陷。它总是从固定值开始动画。

#button-border {
    stroke-dasharray: 150;
    stroke-dashoffset: 150;
    stroke-width: 4px;
    -webkit-animation: dash-back 1.0s linear;
    fill: none;
    pointer-events: all;
}
#button-border:hover {
    -webkit-animation: dash 1.0s linear forwards;
    pointer-events: all;
}

@-webkit-keyframes dash {
    to {
        stroke-dashoffset: 0;
    }
}

@-webkit-keyframes dash-back {
    from {
        stroke-dashoffset: 0;
    }
    to {
        stroke-dashoffset: 150;
    }
}

当鼠标移出按钮以平滑动画时,有没有办法从当前动画帧开始动画?

1 个答案:

答案 0 :(得分:0)

如果使用transitions代替animations来执行相反的操作呢?



#button-border {
  stroke-dasharray: 150;
  stroke-dashoffset: 150;
  stroke-width: 4px;
  -webkit-animation: dash-back 1.0s linear;
  animation: dash-back 1.0s linear;
  fill: none;
  pointer-events: all;
  transition: stroke-dashoffset 1s linear;
  -webkit-transition: stroke-dashoffset 1s linear;
}
#button-border:hover {
  stroke-dashoffset: 0;
  pointer-events: all;
}
@-webkit-keyframes dash-back {
  from {
    stroke-dashoffset: 0;
  }
  to {
    stroke-dashoffset: 150;
  }
}
@keyframes dash-back {
  from {
    stroke-dashoffset: 0;
  }
  to {
    stroke-dashoffset: 150;
  }
}

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100.00000" height="50.00000" id="svg1" version="1.1" viewBox="0 0 100 50" enable-background="new 0 0 100 50" xml:space="preserve">
  <style type="text/css">
    <![CDATA[]]>
  </style>
  <g id="button-border">
    <path class="path" style="fill:none;stroke:#000000;stroke-opacity:1" d="m 100,50.0 0,-50.00000 -100,00.00000" id="path2983" />
    <path class="path" style="fill:none;stroke:#000000;stroke-opacity:1" d="m 0,0 0,50 100,0" id="path2984" />
    <text x="30" y="30" font-family="Verdana" font-size="15" fill="blue">Hello</text>
  </g>
</svg>
&#13;
&#13;
&#13;