【问题标题】:CSS Animation changes the element positionCSS Animation 改变元素位置
【发布时间】:2017-02-18 22:44:48
【问题描述】:

我在一个圆圈内有一个六边形。

查看初始场景:https://jsfiddle.net/njzwfvpf/

现在我想添加一个将六边形在圆圈内旋转 360 度的动画

这是带有 css 动画 (fiddle here) 的现有场景:

.rotate {
  -webkit-animation: rotating 3s cubic-bezier(0, 0, 1, 1) infinite alternate;
          animation: rotating 3s cubic-bezier(0, 0, 1, 1) infinite alternate;
  -webkit-transform-origin: 50% 50%;
          transform-origin: 50% 50%;
}
@-webkit-keyframes rotating {
  100% {
    -webkit-transform: rotate(360deg);
            transform: rotate(360deg);
  }
}
<svg width="350" height="350">
  <svg width="350" height="350">
    <circle cx="230.00591443779982" cy="155.55555555555554" r="45" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
    <circle cx="100" cy="155.55555555555554" r="45" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
    <circle cx="180.41237113402062" cy="155.55555555555554" r="50" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
    <circle cx="165.0943396226415" cy="120.27491408934708" r="45" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
    <circle cx="165.0943396226415" cy="200" r="45" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
    <svg viewBox="0 0 600 720" width="295" height="205">
      <g transform="translate(239, 370)" class="rotate">
        <path></path>
        <path d="M 175 0 L 326.55444566227675 87.50000000000001 L 326.55444566227675 262.5 L 175 350 L 23.445554337723223 262.5 L 23.44555433772325 87.49999999999999" style="opacity: 0.4; fill: black;"></path>
      </g>
      <svg viewBox="0 0 600 720" width="507.4" height="799.5">
        <g transform="translate(105, 395)">
          <path d="M 175 0 L 341.43489035165186 120.9220259843842 L 277.8624191511828 316.57797401561584 L 72.13758084881722 316.57797401561584 L 8.565109648348141 120.92202598438415" style="opacity: 0.4; fill: black;"></path>
        </g>
      </svg>
    </svg>
  </svg>
</svg>

问题是添加动画后六边形改变了它的位置,它不再在圆圈内了。

我做错了什么?我该如何解决?

【问题讨论】:

    标签: css svg css-animations svg-animate


    【解决方案1】:

    您面临的问题是您正在应用 CSS (rotate(360deg)) 的转换,而您已经对 SVG (translate(239, 370)) 中的同一元素进行了转换。您可以通过 2 种方法解决此问题:

    1. 使用 CSS 应用所有变换,以便您可以使用 CSS 控制动画
    2. 在子元素(六边形)上应用 CSS 动画

    以下是如何使用第二种方法解决问题的示例:

    .rotate {
      -webkit-animation: rotating 3s cubic-bezier(0, 0, 1, 1) infinite alternate;
              animation: rotating 3s cubic-bezier(0, 0, 1, 1) infinite alternate;
      -webkit-transform-origin: 50% 50%;
              transform-origin: 50% 50%;
    }
    @-webkit-keyframes rotating {
      100% {
        -webkit-transform: rotate(360deg);
                transform: rotate(360deg);
      }
    }
    <svg width="350" height="350">
      <svg width="350" height="350">
        <circle cx="230.00591443779982" cy="155.55555555555554" r="45" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
        <circle cx="100" cy="155.55555555555554" r="45" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
        <circle cx="180.41237113402062" cy="155.55555555555554" r="50" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
        <circle cx="165.0943396226415" cy="120.27491408934708" r="45" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
        <circle cx="165.0943396226415" cy="200" r="45" style="stroke: rgb(158, 157, 158); stroke-width: 1; fill: none;"></circle>
        <svg viewBox="0 0 600 720" width="295" height="205">
          <g transform="translate(239, 370)">
            <path></path>
            <path class="rotate" d="M 175 0 L 326.55444566227675 87.50000000000001 L 326.55444566227675 262.5 L 175 350 L 23.445554337723223 262.5 L 23.44555433772325 87.49999999999999" style="opacity: 0.4; fill: black;"></path>
          </g>
          <svg viewBox="0 0 600 720" width="507.4" height="799.5">
            <g transform="translate(105, 395)">
              <path d="M 175 0 L 341.43489035165186 120.9220259843842 L 277.8624191511828 316.57797401561584 L 72.13758084881722 316.57797401561584 L 8.565109648348141 120.92202598438415" style="opacity: 0.4; fill: black;"></path>
            </g>
          </svg>
        </svg>
      </svg>
    </svg>

    【讨论】:

    • 也许这个解决方案在过去可能有效,但是当我在您的解决方案中点击运行代码 sn-p 时,它会遇到同样的问题,即多边形不在圆内旋转。我的 svg 旋转动画也有同样的问题。同样在我的情况下,最初没有应用这样的转换,我只是直接在子元素上使用 CSS,但输出不像在这种情况下那样预期。在努力解决之后,我什至在设计中从旋转动画切换到缩放动画,但面临同样的问题。元素的中心位置随着这些动画而变化。那么我们该如何解决呢?
    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    相关资源
    最近更新 更多