简体   繁体   English

Firefox忽略SVG中的内联CSS,Chrome将其显示

[英]Firefox ignores inline CSS in SVG, chrome displays it

I've been working on an animated logo in SVG, it's pretty simple. 我一直在用SVG制作动画徽标,这很简单。 I trigger it with a javascript function, animate(). 我使用javascript函数animate()触发它。

When i run animate() in chrome, the styles successfully apply and the logo displays, but when i do the same in firefox it fails. 当我在chrome中运行animate()时,样式将成功应用并显示徽标,但是当我在Firefox中执行相同操作时,它将失败。

i also noted that the inline block in my SVG is not applied, regardless of where i place it in the document, hence i concluded it must be a problem with inline CSS. 我还注意到,无论我将其放置在文档中的什么位置,都不会应用我的SVG中的内联块,因此,我得出结论认为内联CSS一定是有问题的。

Here is my current code : 这是我当前的代码:

 <svg xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100" id="svg8"> <defs> <style> .rect{ height:0px; transition:height 1s ease;} </style> </defs> <g id="layer1" transform="translate(0 -197)" fill-opacity=".617" stroke-width="13.229"> <rect class="rect" id="logo-2" width="16.611" x="-120.759" y="-266.945" ry="8.305" transform="rotate(-165)" opacity=".71" fill="#ff584a"/> <rect class="rect" id="logo-1" width="16.611" x="-40.446" y="205.589" ry="8.305" transform="rotate(-15)" opacity=".71" fill="#ff584a"/> <rect class="rect" id="logo-3" width="16.611" x="-6.786" y="214.383" ry="8.305" transform="rotate(-15)" opacity=".71" fill="#0a3fa6"/> </g> <script> function animate(){ document.getElementById('logo-1').style="height:83.055px;"; document.getElementById('logo-2').style="height:83.055px;"; document.getElementById('logo-3').style="height:83.055px;"; console.log("IT WORKS"); } </script> </svg> 

Interestingly in the firefox inspector, the CSS is applied, however in the document itsself nothing changes... 有趣的是,在firefox检查器中使用了CSS,但是在文档本身中没有任何变化...

I'm not sure if this is a bug with firefox or intended behavior, or i'm doing something wrong in my code, any help would be much appreciated ! 我不确定这是否是firefox的错误或预期的行为,或者我的代码做错了,任何帮助将不胜感激!

Having width, height, x and y of rect elements be CSS properties that can be animated with CSS animations is a new feature of the SVG 2 specification. rect元素的width,h,x和y是CSS属性,可以使用CSS动画进行动画处理是SVG 2规范的新功能。 In SVG 1.1 such things were attributes and could only be animated with javascript and SMIL. 在SVG 1.1中,这些东西是属性,只能使用javascript和SMIL进行动画处理。

Fortunately Firefox has just implemented this part of SVG 2 and from Firefox 69 this will work as you wish. 幸运的是,Firefox刚刚实现了SVG 2的这一部分,而从Firefox 69开始,它将按您的意愿工作。 You can try a nightly right now if you wish to test it. 如果您想测试一下,可以立即尝试每晚进行一次。

You can either wait or convert the animation to SMIL. 您可以等待,也可以将动画转换为SMIL。

As Robert Longson mention is his answer, for now you can't animate the height of SVG elements using CSS. 正如罗伯特·朗森(Robert Longson)提到的那样,现在,您无法使用CSS设置SVG元素的高度。

This is how you can do it using SMIL animations: 这是使用SMIL动画的方法:

 <svg viewBox="0 0 100 100" id="svg8"> <style type="text/css"> <![CDATA[ .rect{ stroke:black;} ]]> </style> <g id="layer1" transform="translate(0 -197)" fill-opacity=".617" stroke-width="13.229"> <rect class="rect" id="logo-2" width="16.611" x="-120.759" y="-266.945" ry="8.305" height="0" transform="rotate(-165)" opacity=".71" fill="#ff584a"> <animate attributeName="height" attributeType="XML" values="0;83.055" dur=".5s" fill="freeze"/> </rect> <rect class="rect" id="logo-1" width="16.611" x="-40.446" y="205.589" ry="8.305" height="0" transform="rotate(-15)" opacity=".71" fill="#ff584a"> <animate attributeName="height" attributeType="XML" values="0;83.055" dur=".5s" fill="freeze"/> </rect> <rect class="rect" id="logo-3" width="16.611" x="-6.786" y="214.383" ry="8.305" height="0" transform="rotate(-15)" opacity=".71" fill="#0a3fa6"> <animate attributeName="height" attributeType="XML" values="0;83.055" dur=".5s" fill="freeze"/> </rect> </g> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM