使用CSS动画从中心缩放SVG

2022-03-10 00:00:00 svg css css-animations svg-animate

我阅读了几篇关于从中心缩放SVG路径的文章,但都不起作用。

    div { width: 100px; height: 100px; background: rgba(0,0,0,0.2)}
    
        #circle {
          transform: scale(1) translate(0px, 0px);
          animation: circle .5s linear 1.3s forwards;
        }
    
        @keyframes circle {
          to {
            transform: scale(4.1) translate(-38px, -38px);
          }
        }
    <div>
      <svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
          <g id="circle" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
              <circle id="Oval" fill="#2A2C32" cx="50" cy="50" r="11.5"></circle>
          </g>
      </svg>
    </div>

Fiddle


解决方案

使用Transform-Origin和Transform-Box,避免您正在进行的所有翻译猜测。

div { width: 100px; height: 100px; background: rgba(0,0,0,0.2)}
    
        #circle {
          transform: scale(1);
          transform-box: fill-box;
          transform-origin: center;
          animation: circle .5s linear 1.3s forwards;
        }
    
        @keyframes circle {
          to {
            transform: scale(4.1);
          }
        }
<div>
      <svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
          <g id="circle" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
              <circle id="Oval" fill="#2A2C32" cx="50" cy="50" r="11.5"></circle>
          </g>
      </svg>
    </div>

相关文章