【问题标题】:CSS how to revert SVG animation on mouseout from current frameCSS如何从当前帧恢复鼠标悬停的SVG动画
【发布时间】:2015-04-27 13:51:07
【问题描述】:

我正在使用 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;
    }
}

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

【问题讨论】:

    标签: css animation svg


    【解决方案1】:

    使用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>

    【讨论】:

    • 谢谢您,先生!你拯救了我的一天,这正是我想要的。但是过渡属性对动画有什么影响呢?你能解释一下你是怎么想出来的吗?您使用了什么文档?
    • 我认为过渡会以某种方式覆盖动画:例如,如果您将动画从 100 设置为 0 ,持续时间为 3s1s 的过渡,您将拥有值从0 动画到1s 中的100,然后跳转到33,然后在3 秒内动画到0。但是,我还没有阅读所有规范(animationtransitions),只是从虚拟测试中得到的。
    • 我会读的。但这对我来说太令人困惑了。我认为你的解释是一个很好的起点。我低估了过渡。
    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 2019-09-01
    • 1970-01-01
    • 2020-09-26
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    相关资源
    最近更新 更多