CSS 过渡动画无法在 SVG 路径的“d”属性变化时生效。

7
1个回答

12

过渡只能应用于演示属性,以及一些其他属性,例如x、y、cx、cy等。可以在此处找到支持的属性列表。不幸的是,d不在其中...

由于这仍然无法在各种浏览器中可靠地支持,因此您可以使用SMIL动画来实现相同的效果。

var type = true;

setInterval(function() {
    $("#path").attr('from', type ? "M 0 0 L 50 100" : "M 0 0 L 100 50");
    $("#path").attr('to', type ? "M 0 0 L 100 50" : "M 0 0 L 50 100");
    $("#path")[0].beginElement()
    $("#circle").attr('from', type ? "40" : "10");
    $("#circle").attr('to', type ? "10" : "40");
    $("#circle")[0].beginElement()
    type = !type;
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg>
    <path stroke="#000000" stroke-width="5" d="M 0 0 L 100 50" >  
        <animate id="path" attributeName="d" from="M 0 0 L 100 50" to="M 0 0 L 50 100" dur="1s" fill="freeze"/>
    </path>
    <circle fill="#0000FF" cx="10" cy="50" r="10" >
        <animate id="circle" attributeName="cx" from="10" to="40" dur="1s" fill="freeze"/>
    </circle>
</svg>


1
我正在重构我的所有代码,以使用svg:line代替svg:path...该死... - jk-kim
https://jsfiddle.net/rnhu7673/4/ 已更改为线条,尽管x2和y2在文档中,但它不会动画化。 - jk-kim
1
你是对的,链接的网站只是一个提案,并非所有属性都被所有浏览器支持。例如,在Firefox中,你的fiddle根本不起作用。在这种情况下,最安全的方法是使用SMIL,以便在转换无法工作时使用... - Holger Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接