为什么“transform:rotate(Xdeg)”会发生变化?导致动画停止?
当窗口达到最小/最大大小时,我尝试旋转动画字体箭头,但是当旋转发生时,动画停止,也仅用于测试,我尝试替换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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么会发生这种情况
变换属性对于许多变换函数是“共享的”,并且CSS不组合任何属性的值。
由于您的动画是使用
transform:translateX(..)
制作的,因此添加transform:rotate(..)
将覆盖属性的值,而不是组合它们。即生成的样式是transform:rotate(..)
,而不是transform:translateX(..)rotate(..)
。如果您对
box-shadow
进行动画处理,然后还想要一个插入box-shadow
,情况会是一样的,它会用另一个覆盖一个。或者更简单 - 如果你有.box { color: red;颜色: 蓝色; }
css 将选择最后一个值(蓝色)应用于color
属性。如果有CSS属性
rotate: 90deg
和translate: 4px
(只有未得到广泛支持),那么您的动画就可以工作,因为平移动画将应用于与旋转不同的属性,而不是覆盖许多变换函数本质上共享的一个。解决方法
解决这个问题的方法有很多。
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(..)
, addingtransform: rotate(..)
will overwrite the property's value, not combine them. I.e. the resulting style istransform: rotate(..)
, nottransform: translateX(..) rotate(..)
.It would be the same if you were animating the
box-shadow
and then wanted an insetbox-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 thecolor
property.If there were css properties
rotate: 90deg
andtranslate: 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.