简体   繁体   English

svg 子元素上的 CSS 变换原点问题

[英]CSS transform origin issue on svg sub-element

I'm having issues with the transform-origin while attempting to scale sub-elements.我在尝试缩放子元素时遇到了转换原点的问题。

While attempting to scale animate a box within a larger svg, it uses the transform origin (0,0) from the overall svg, rather than the center of the element I am trying to scale.在尝试在更大的 svg 中缩放动画框时,它使用来自整个 svg 的变换原点 (0,0),而不是我尝试缩放的元素的中心。

This makes it appear like it is "flying in from the top left" which is not what I am looking for.这使它看起来像是“从左上角飞入”,这不是我想要的。 I am looking to make it scale from the elements center.我希望让它从元素中心缩放。

How do I get the transform-origin to be set relative to the specific element I am animating, without having to hardcode the (x,y) position of the sub-element itself.如何相对于我正在设置动画的特定元素设置变换原点,而不必硬编码子元素本身的 (x,y) 位置。

Here is a simple example of the issue I'm dealing这是我正在处理的问题的一个简单示例

 @keyframes scaleBox { from {transform: scale(0);} to {transform: scale(1);} } #animated-box { animation: scaleBox 2s infinite; }
 <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" style=" width: 195px; "><defs> <style>.cls-1{fill:#7f7777;}.cls-2{fill:#fff;}</style> </defs> <rect class="cls-1" x="0.5" y="0.5" width="99" height="99"></rect> <path d="M99,1V99H1V1H99m1-1H0V100H100V0Z"></path> <rect id="animated-box" class="cls-2" x="10.5" y="8.5" width="22" height="6"></rect></svg>

You need transform-box: fill-box;你需要变换框:填充框;

 @keyframes scaleBox { from {transform: scale(0);} to {transform: scale(1);} } #animated-box { transform-box: fill-box; animation: scaleBox 2s infinite; }
 <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" style=" width: 195px; "><defs> <style>.cls-1{fill:#7f7777;}.cls-2{fill:#fff;}</style> </defs> <rect class="cls-1" x="0.5" y="0.5" width="99" height="99"></rect> <path d="M99,1V99H1V1H99m1-1H0V100H100V0Z"></path> <rect id="animated-box" class="cls-2" x="10.5" y="8.5" width="22" height="6"></rect></svg>

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

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