每个 <g> 的 CSS 动画 'origin'

标签 css animation sass

使用 CSS 动画,我为单词中的每个字母添加了“摆动”效果。每个字母由一个SVG组组成<g> .但是,正如您在示例中看到的那样,每个字母的效果都变得更加极端,而我希望每个字母都有一致的“摆动”(对每个字母产生相同的效果)。如何实现?

注意:为了保持问题整洁,我没有包含 SVG 源代码。需要的话可以在示例中看到。

谢谢。

SCSS

// Logo
.logo {
    position: fixed;
    top: 50%;
    left: 50%;
  transform: translate(-50%,-50%);
    z-index: 1;
    width: 260px;
    display: block;

    // SVG
    svg {
        display: block;
        width: 100%;
        overflow: visible;

        g {
            fill: transparent;
            transition: all 300ms ease-in-out;

            @keyframes wobble {
              0% { transform: rotate(0) translate3d(0, 0, 0) }
              25% { transform: rotate(2deg) translate3d(1px, 0, 0) }
              50% { transform: rotate(-1deg) translate3d(0, -1px, 0) }
              75% { transform: rotate(1deg) translate3d(-1px, 0, 0) }
              100% { transform: rotate(-2deg) translate3d(-1px, -1px, 0) }
            }

            animation-duration: 400ms;
            animation-iteration-count: infinite;
            animation-fill-mode: none;
            animation-name: wobble;
            animation-timing-function: ease-in-out;

            path {
                fill: red;
            }
        }
  }
}

Example

最佳答案

我无法弄清楚如何使用 SVG 来做到这一点 - 我确实设法提出了与您的要求类似的东西。

部分解决方案涉及使用旋转中心点:

transform-origin: center;

查看下面的演示

#my-logo div {
  display: inline-block;
  color: red;
  font-size: 60px;
  font-family: arial;
  font-weight: bolder;
  text-transform: uppercase;
  fill: transparent;
  transition: all 300ms ease-in-out;
  transform-origin: center;
  animation-duration: 400ms;
  animation-iteration-count: infinite;
  animation-fill-mode: none;
  animation-name: wobble;
  animation-timing-function: ease-in-out;
}

@keyframes wobble {
  0% {
    transform: rotate(0) translate3d(0, 0, 0);
  }
  25% {
    transform: rotate(2deg) translate3d(1px, 0, 0);
  }
  50% {
    transform: rotate(-1deg) translate3d(0, -1px, 0);
  }
  75% {
    transform: rotate(1deg) translate3d(-1px, 0, 0);
  }
  100% {
    transform: rotate(-2deg) translate3d(-1px, -1px, 0);
  }
}
<div id="my-logo">
  <div>o</div>
  <div>u</div>
  <div>t</div>
  <div>r</div>
  <div>a</div>
  <div>g</div>
  <div>e</div>
  <div>
    <!-- also works with images -->
    <img src="http://placekitten.com/100/100" />
  </div>
</div>

关于每个 <g> 的 CSS 动画 'origin',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629353/

相关文章:

javascript - 根据浏览器宽度通过平滑过渡更改网格中的列数

css - 如何同时为具有一个计时功能的 translateX 添加转换和为具有另一个计时功能的 translateY 添加转换?

wpf - 多个目标名称的相同动画

css - grunt dos NOT create css (sass to css) : Done, 没有错误

html - 如果不进行调整大小更改,我该如何工作?

html - 移动设备上的按列响应表

javascript - jQuery addClass 基于滚动距离

css - 如何在CSS文件中添加if-else逻辑?

css - 使用node-sass查看整个目录时,请指定输出文件名

javascript - 在所有页面中包含 Menu.html