39

按照这个话题:

Firefox 38-40 SMIL 问题 - 速度非常慢(在 22.09.15 的 FF 版本 41 中解决)

和这个话题:

打算弃用:SMIL

SVG 标签“animateTransform”不能正常工作。用 CSS 或 CSS 过渡替换 SMIL(动画标签)会很好。

CONSOLE WARNING: Please use CSS animations or Web animations instead),
which would work fast on the latest versions of Firefox and Chrome.

下一个谷歌浏览器警告:

CONSOLE WARNING: SVG's SMIL animations ('animate', 'set', etc.) are deprecated 
and will be removed. Please use CSS animations or Web animations instead.

修订版 196823:添加 SMIL 弃用警告


首先,我需要实现三件事:

1)鼠标悬停效果(最简单)

它怎么样:

<rect x="-0.5" y="-0.5" width="1" height="1" fill="white">
    <!--it makes half-visible selecting effect -->
    <set attributeName="stroke-opacity" begin="mouseover" end="mouseout" to="0.5"/>
    <!-- explicitly reverse the opacity animation on mouseout -->
    <set attributeName="stroke-opacity" begin="mouseout" end="mouseover" to="1"/>
</rect>

我删除了set标签,将类rect添加到标签中,并将其添加到 CSS 悬停伪类中:

.element_tpl:hover {
    stroke-opacity: 0.5;
}

2)在对这个元素(页面加载)进行更改后,它会缩放几次

它怎么样:

<!--animation-->
<!--it scales a few times after change committed to this element -->
<animateTransform attributeType="XML" attributeName="transform" type="scale" dur="0.5s" keyTimes="0;0.5;0.5;1" values="1;1.12;1.12;1" repeatCount="6" fill="freeze"/>

如何在没有animate标签的情况下组织:

???


3)它动画放大和缩小(onclick)

它怎么样:

<!--it animates scale up and scale down onclick -->
    <animateTransform attributeName="transform" attributeType="XML" type="scale" from="1" to="1.15" repeatCount="1" begin="mousedown+0.2s" dur = "0.2s" fill="freeze"/>
    <animateTransform attributeName="transform" attributeType="XML" type="scale" from="1.15" to="1" repeatCount="1" begin="mouseup+0.4s" dur = "0.2s" fill="freeze"/>

animate没有标签如何组织?尝试使用:active,但行为有所不同:

.element_tpl:active {
    transform: scale(1.1); 
}

这是我的模板元素的完整代码:

<g id="switcher" cursor="pointer" stroke-width="0.15">
    <g transform="scale(1,1.375)">
        <g>
            <rect x="-0.5" y="-0.5" width="1" height="1" stroke="white" pointer-events="none"/>
            <rect x="-0.5" y="-0.5" width="1" height="1" fill="white">
                <!--it makes half-visible selecting effect -->
                <set attributeName="stroke-opacity" begin="mouseover" end="mouseout" to="0.5"/>
                <!-- explicitly reverse the opacity animation on mouseout -->
                <set attributeName="stroke-opacity" begin="mouseout" end="mouseover" to="1"/>
            </rect>
            <line x1="0" y1="-0.25" x2="0" y2="0.25" stroke-width="0.17" stroke-linecap="round" pointer-events="none"/><!-- vertical on -->
            <!--animation-->
            <!--it scales a few times after change committed to this element -->
            <animateTransform attributeType="XML" attributeName="transform" type="scale" dur="0.5s" keyTimes="0;0.5;0.5;1" values="1;1.12;1.12;1" repeatCount="6" fill="freeze"/>
            <!--it animates scale up and scale down onclick -->
            <animateTransform attributeName="transform" attributeType="XML"
            type="scale" from="1" to="1.15" repeatCount="1" begin="mousedown+0.2s" dur = "0.2s"
            fill="freeze"/>
            <animateTransform attributeName="transform" attributeType="XML"
            type="scale" from="1.15" to="1" repeatCount="1" begin="mouseup+0.4s" dur = "0.2s"
            fill="freeze"/>
        </g>
    </g>
</g>

我当前工作项目的工作版本如下所示:

http://jsfiddle.net/7e2jeet0(以前仅由浏览器 FF 使用 - 因为(注意)悬停在这里使用 2 个数字 - 导致 [Chrome 支持 SMIL 和 'use' 一起,Firefox 目前不支持 SMIL 和 'use ' 一起] / 根据罗伯特·朗森)

在我尝试制作等效的 CSS 时,它看起来像

http://jsfiddle.net/7e2jeet0/1/(在FF中)

http://jsfiddle.net/7e2jeet0/2/(在 Chrome 中)


或其他元素相同。工作版本:

http://jsfiddle.net/f7o03rsr/

http://jsfiddle.net/f7o03rsr/1/

http://jsfiddle.net/f7o03rsr/2/

谢谢!


编辑 1

我发现这种组合变体适用于 Firefox 中的悬停和鼠标按下,但只有悬停效果在 Chrome 中有效。


我也对如何保存其中一些动画感兴趣:

http://jsfiddle.net/e4dxx2wg/

http://jsfiddle.net/e4dxx2wg/1/

通过将它们转移到 CSS / Web 动画?

4

1 回答 1

10

SMIL 支持并未从 Chrome 中删除,而是被 Polyfill 取代。Eric Willigers 创建了一个完全在 Web Animations API 上实现的 SMIL polyfill。你可以在这里找到它: https ://github.com/ericwilligers/svg-animation

于 2015-08-14T10:13:32.237 回答