为什么“transform:rotate(Xdeg)”会发生变化?导致动画停止?

发布于 2025-01-18 06:31:16 字数 1967 浏览 1 评论 0原文

当窗口达到最小/最大大小时,我尝试旋转动画字体箭头,但是当旋转发生时,动画停止,也仅用于测试,我尝试替换transform:rotate(90DEG) to <<代码>转换:旋转(0DEG)维护同一箭头的方向,但也会导致停止动画。问题是变换:rotate(),可以通过检查元素并激活/停用它在浏览器开发人员工具中来轻松测试。

绕过这一点的一种简单方法可以使用两个&lt; p&gt;每个箭都在不同方向,垂直和水平动画,以及使用display> display:none;>当最小/最大尺寸切换时它们之间交替,但是我想要的是知道为什么会发生这种情况以及如何使用此方法解决此问题

.text-center {
  text-align: center;
}

.lnr-x3 {
  font-size: 2.4rem;
}

@media (max-width: 991px) {
  #catalogArrow_h {
    transform: rotate(90deg) !important;
    transform-origin: center !important;
  }
}

.animated-h {
  text-decoration: none;
  outline-style: none;
  -webkit-animation: movingHorizontally 1.7s ease-in-out infinite;
  animation: movingHorizontally 1.7s ease-in-out infinite;
}

@keyframes movingHorizontally {
  0% {
    transform: translateX(0px);
    -webkit-transform: translateX(0px);
  }
  50% {
    transform: translateX(-10px);
    -webkit-transform: translateX(-10px);
  }
  100% {
    transform: translateX(0px);
    -webkit-transform: translateX(0px);
  }
}

@-webkit-keyframes movingHorizontally {
  0% {
    transform: translateX(0px);
    -webkit-transform: translateX(0px);
  }
  50% {
    transform: translateX(-10px);
    -webkit-transform: translateX(-10px);
  }
  100% {
    transform: translateX(0px);
    -webkit-transform: translateX(0px);
  }
}
<!-- Font Icons -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" type="text/css">
<link rel="stylesheet" href="https://cdn.linearicons.com/free/1.0.0/icon-font.min.css">

<div class="col-12">
  <p class="text-center pt-3 px-5">
    <span id="catalogArrow_h" class="lnr lnr-x3 lnr-arrow-right fas animated-h"></span>
  </p>
</div>

I tried to rotate an animated font arrow when the window reached a min/max size, but when the rotate takes place the animation stops, also just for testing I tried replacing transform: rotate(90deg) to transform: rotate(0deg) which maintains the same arrow's direction but it causes to stop the animation too. The issue is with transform: rotate() and it can be easily tested by inspecting the element and activating/deactivating it in the browsers developer tools.

An easy way to bypass this can be using two <p> each one with an arrow in different direction and with vertical and horizontal animation each, and using display: none; to alternate between them when the min/max size switches, but what I want is to know why this is happening and how to solve this using this approach

.text-center {
  text-align: center;
}

.lnr-x3 {
  font-size: 2.4rem;
}

@media (max-width: 991px) {
  #catalogArrow_h {
    transform: rotate(90deg) !important;
    transform-origin: center !important;
  }
}

.animated-h {
  text-decoration: none;
  outline-style: none;
  -webkit-animation: movingHorizontally 1.7s ease-in-out infinite;
  animation: movingHorizontally 1.7s ease-in-out infinite;
}

@keyframes movingHorizontally {
  0% {
    transform: translateX(0px);
    -webkit-transform: translateX(0px);
  }
  50% {
    transform: translateX(-10px);
    -webkit-transform: translateX(-10px);
  }
  100% {
    transform: translateX(0px);
    -webkit-transform: translateX(0px);
  }
}

@-webkit-keyframes movingHorizontally {
  0% {
    transform: translateX(0px);
    -webkit-transform: translateX(0px);
  }
  50% {
    transform: translateX(-10px);
    -webkit-transform: translateX(-10px);
  }
  100% {
    transform: translateX(0px);
    -webkit-transform: translateX(0px);
  }
}
<!-- Font Icons -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" type="text/css">
<link rel="stylesheet" href="https://cdn.linearicons.com/free/1.0.0/icon-font.min.css">

<div class="col-12">
  <p class="text-center pt-3 px-5">
    <span id="catalogArrow_h" class="lnr lnr-x3 lnr-arrow-right fas animated-h"></span>
  </p>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

司马昭之心 2025-01-25 06:31:16

为什么会发生这种情况

变换属性对于许多变换函数是“共享的”,并且CSS不组合任何属性的值。

由于您的动画是使用 transform:translateX(..) 制作的,因此添加 transform:rotate(..) 将覆盖属性的值,而不是组合它们。即生成的样式是 transform:rotate(..),而不是 transform:translateX(..)rotate(..)

如果您对 box-shadow 进行动画处理,然后还想要一个插入 box-shadow,情况会是一样的,它会用另一个覆盖一个。或者更简单 - 如果你有 .box { color: red;颜色: 蓝色; } css 将选择最后一个(蓝色)应用于color 属性。

如果有CSS属性rotate: 90degtranslate: 4px(只有未得到广泛支持),那么您的动画就可以工作,因为平移动画将应用于与旋转不同的属性,而不是覆盖许多变换函数本质上共享的一个。

解决方法

解决这个问题的方法有很多。

  • 您可以在父元素上设置平移或旋转
<div class="rotate-90">
  <span class="translate-animate"></span>
</div>
  • 您可以将旋转添加到平移动画属性中:
@keyframes movingHorizontallyRotated {
  0%, 100% { transform: translateX(0px)   rotate(90deg); }
  50%      { transform: translateX(-10px) rotate(90deg); }
}
  • 您可以对不同的属性进行动画处理以平移元素:
@keyframes movingHorizontally {
  0%, 100% { padding: 5px 10px 5px 0px; }
  50%      { padding: 5px 0px 5px 10px; }
}
  • 如果您的框架/资产提供了一个已经旋转的箭头,则可以使用/制作一个已经旋转的箭头。

Why does this happen

The transform property is "shared" for many transform functions and css doesn't combine any property's values.

Because your animation is made with transform: translateX(..), adding transform: rotate(..) will overwrite the property's value, not combine them. I.e. the resulting style is transform: rotate(..), not transform: translateX(..) rotate(..).

It would be the same if you were animating the box-shadow and then wanted an inset box-shadow too, it would overwrite one with the other. Or more simply - if you have .box { color: red; color: blue; } css will choose the last value (blue) to apply to the color property.

If there were css properties rotate: 90deg and translate: 4px (there are but not widely supported), then your animation would work, because the translate animation would be applying to a different property than the rotation, not overwriting one that is essentially shared amongst many transform functions.

Ways around it

There are many ways around this problem.

  • You can set the translate or rotate on the parent element
<div class="rotate-90">
  <span class="translate-animate"></span>
</div>
  • You can add the rotate to your translate animation properties:
@keyframes movingHorizontallyRotated {
  0%, 100% { transform: translateX(0px)   rotate(90deg); }
  50%      { transform: translateX(-10px) rotate(90deg); }
}
  • You can animate a different property to translate the element:
@keyframes movingHorizontally {
  0%, 100% { padding: 5px 10px 5px 0px; }
  50%      { padding: 5px 0px 5px 10px; }
}
  • You can use/make an already rotated arrow if your framework/ assets provides one.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文