【问题标题】:svg path shifts from it's normal position while rotatingsvg 路径在旋转时从其正常位置移动
【发布时间】:2021-05-03 18:42:24
【问题描述】:

我有一个 svg 时钟,当我旋转它从正常位置移动的手时,#minute-hand element transform-b​​ox 属性在这个 svg 元素中不起作用

注意:此 svg 代码由 illustrator 生成

以及codepen 上的另一个演示

@keyframes rotate-hand{
  from{transform: rotate(0deg);}
  to{transform: rotate(360deg)}
}

/* comment this to see the original hands-shape*/
#seconds-hand, #minutes-hand, #hours-hand{
    animation: rotate-hand 5s linear infinite;
    transform-box: fill-box;
}

#seconds-hand{
/*   animation: rotate-hand 5s linear infinite; */
/*   transform-box: fill-box; */
}

#minutes-hand{
  
}

#hours-hand{
  
}
<svg xmlns="http://www.w3.org/2000/svg" width="170.5" height="170.5" viewBox="0 0 170.5 170.5" overflow="scroll">
    <path id="border_1_"
          d="M85.3 0C38.2 0 0 38.2 0 85.3s38.2 85.3 85.3 85.3 85.3-38.2 85.3-85.3S132.3 0 85.3 0zm-1 160c-40.6 0-73.5-32.9-73.5-73.5S43.7 13 84.3 13s75.5 32.9 75.5 73.5-35 73.5-75.5 73.5z"
          fill="#002745"/>

  
    <path id="seconds-hand" transform="rotate(134.355 99.90008916 100.15364826)" fill="#2fc0ea"
          d="M99.1 78.9h1.5v42.5h-1.5z"/>
  
  
    <path id="hours-hand" d="M86.2 88.3h-2.5c-.5 0-1-.4-1-1V48.7c0-.5.4-1 1-1h2.5c.5 0 1 .4 1 1v38.6c0 .6-.5 1-1 1z"
          fill="#002745"/>
  
  
    <path id="minutes-hand"
          d="M69.2 102.7c-.9-.9-.9-2.3-.1-3.1l14.2-14.7c.9-.9 2.3-.9 3.1-.1.9.9.9 2.3.1 3.1l-14.2 14.7c-.8.9-2.2.9-3.1.1z"
          fill="#002745"/>
  
</svg>

【问题讨论】:

    标签: css svg svg-animate


    【解决方案1】:

    transform-origin:center 是您需要的,但您必须考虑已应用于seconds-hand 的转换。我为它添加了一个g 元素

    @keyframes rotate-hand{
      from{transform: rotate(0deg);}
      to{transform: rotate(360deg)}
    }
    
    /* comment this to see the original hands-shape*/
    #seconds-hand, #minutes-hand, #hours-hand{
        animation: rotate-hand 5s linear infinite;
        transform-origin:center;
    }
    #minutes-hand {
      animation-duration:2s;
    }
    #seconds-hand {
      animation-duration:1s;
    }
    <svg xmlns="http://www.w3.org/2000/svg" width="170.5" height="170.5" viewBox="0 0 170.5 170.5" overflow="scroll">
        <path id="border_1_"
              d="M85.3 0C38.2 0 0 38.2 0 85.3s38.2 85.3 85.3 85.3 85.3-38.2 85.3-85.3S132.3 0 85.3 0zm-1 160c-40.6 0-73.5-32.9-73.5-73.5S43.7 13 84.3 13s75.5 32.9 75.5 73.5-35 73.5-75.5 73.5z"
              fill="#002745"/>
    
        <g id="seconds-hand">
        <path  transform="rotate(134.355 99.90008916 100.15364826)" fill="#2fc0ea"
              d="M99.1 78.9h1.5v42.5h-1.5z"/>
      
        </g>
        <path id="hours-hand" d="M86.2 88.3h-2.5c-.5 0-1-.4-1-1V48.7c0-.5.4-1 1-1h2.5c.5 0 1 .4 1 1v38.6c0 .6-.5 1-1 1z"
              fill="#002745"/>
      
      
        <path id="minutes-hand"
              d="M69.2 102.7c-.9-.9-.9-2.3-.1-3.1l14.2-14.7c.9-.9 2.3-.9 3.1-.1.9.9.9 2.3.1 3.1l-14.2 14.7c-.8.9-2.2.9-3.1.1z"
              fill="#002745"/>
      
    </svg>

    【讨论】:

    • 我从没想过transform-origin会解决这个问题,但是为什么我们需要在组中添加秒针呢?这在我的完整 svg 代码中不起作用,这个时钟是它的一部分
    • @Fathy 因为已经对秒针应用了一个变换,所以你将用旋转覆盖它,添加一个组将允许我们同时进行变换
    • @Fathy 如果 SVG 不同,我的代码将无法工作,因为中心可能会改变,您需要另一个来源
    • transform-origin 以父 svg 为中心?或它组的中心,我怎样才能将它调整到正确的中心位置
    • @Fathy 使用 transform-origin: X% Y% 并根据您的情况调整 X/Y 值
    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 2019-04-15
    • 2014-02-19
    • 1970-01-01
    • 2012-03-16
    • 2015-02-26
    • 2013-04-21
    • 2016-01-16
    相关资源
    最近更新 更多