【问题标题】:Interrupting CSS animation causes weird transformation中断 CSS 动画会导致奇怪的转换
【发布时间】:2019-08-27 07:44:30
【问题描述】:

我一直在尝试创建“卡片”,当您单击它们到窗口的完整视口时,它们会展开。除了在动画完成之前中断动画之外,它一直进展顺利,它会导致一些图形故障。我正在使用 CSS 转换而不是 Javascript 动画(我只是使用 JQuery 来制作所需的类更改的原型),并且仅在 CSS 中实现也有这个问题。请在此处使用 JSFiddle 链接:http://jsfiddle.net/ny85ebgu/1/,因为似乎代码在 StackOverflow 上无法正常运行。

$(window).on("click", ".small", function() {
    me = this;
    setTimeout( function() { $(me).addClass("big"); }, 1 );
});

$(window).on("click", ".big", function() {
    me = this;
    setTimeout( function() { $(me).removeClass("big"); }, 1 );
});
div.small {
    background: #f00;
    width: calc((100% - 4rem - 4rem) / 3);
    height: calc((100% - 6rem) / 2);
    position: absolute;
    border-radius: 0.5rem;
    z-index: 6;
    top: 2rem;
    left: 2rem;
    transition: all .5s;
}

div.smaller {
    background: black;
    width: calc((100% - 4rem - 4rem) / 3);
    height: calc((100% - 6rem) / 2);
    position: absolute;
    border-radius: 0.5rem;
    z-index: 5;
    top: 2rem;
    left: calc(((100% - 4rem - 4rem) / 3) + 4rem);
    transition: all .5s;
}

div.smallest {
    background: blue;
    width: calc((100% - 4rem - 4rem) / 3);
    height: calc((100% - 6rem) / 2);
    position: absolute;
    border-radius: 0.5rem;
    z-index: 4;
    top: 2rem;
    left: calc((((100% - 4rem - 4rem) / 3) * 2) + 6rem);
    transition: all .5s;
}

div.even-smaller {
    background: green;
    width: calc((100% - 4rem - 4rem) / 3);
    height: calc(((100% - 6rem) / 2));
    position: absolute;
    border-radius: 0.5rem;
    z-index: 3;
    top: calc(((100% - 6rem) / 2) + 4rem);
    left: 2rem;
    transition: all .5s;
}

div.really-small {
    background: pink;
    width: calc((100% - 4rem - 4rem) / 3);
    height: calc((100% - 6rem) / 2);
    position: absolute;
    border-radius: 0.5rem;
    z-index: 2;
    top: calc(((100% - 6rem) / 2) + 4rem);
    left: calc((((100% - 4rem - 4rem) / 3) * 1) + 4rem);
    transition: all .5s;
}

div.amazingly-small {
    background: orange;
    width: calc((100% - 4rem - 4rem) / 3);
    height: calc((100% - 6rem) / 2);
    position: absolute;
    border-radius: 0.5rem;
    z-index: 1;
    top: calc(((100% - 6rem) / 2) + 4rem);
    left: calc((((100% - 4rem - 4rem) / 3) * 2) + 6rem);
    transition: all .5s;
}

div.big {
    width: 100% !important;
    height: 100% !important;
    top: 0 !important;
    margin-top: 0 !important;
    left: 0 !important;
    margin-left: 0 !important;
    border-radius: 0 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
<div class="small"></div>
<div class="small smaller"></div>
<div class="small smallest"></div>
<div class="small even-smaller"></div>
<div class="small really-small"></div>
<div class="small amazingly-small"></div>

您会注意到,当与顶行交互时,当您将其打断回到正常位置时,它似乎从左侧和顶部开始动画,而底行(不包括最左侧的“卡片”)只是弹回。另请注意,我尝试过不使用 calc() 并没有帮助。

我最好让动画反向运行。

编辑:这似乎是 Safari 中的一个问题。在 Chrome 和其他 Chromium 浏览器中运行良好。不过不确定 Firefox。

【问题讨论】:

  • 我没有看到这个问题(Firefox、Win10)。哪个浏览器在哪个平台出现这个问题?
  • 抱歉,请参阅编辑。似乎是 Safari 上的一个问题(我知道),很高兴知道它在 Firefox 中有效。我需要这个才能在 Safari 中工作,所以你知道是什么原因造成的吗?
  • MacOS 上的 Safari,我想?
  • 是的,没错。
  • 哪个 MacOS?哪个 Safari 版本?

标签: javascript jquery html css


【解决方案1】:

这似乎确实是一个 Safari 错误,由使用相对单位触发。
显然,他们无法从当前状态恢复到基于百分比的值。

因此,您需要使用绝对单位来解决该问题。
在您的情况下,您可以简单地将所有初始 width:[..]%[..] 替换为 width:[..]vw[..] 并将所有 height 替换为 vh

$(window).on("click", ".small", function() {
    me = this;
    setTimeout( function() { $(me).addClass("big"); }, 1 );
});

$(window).on("click", ".big", function() {
    me = this;
    setTimeout( function() { $(me).removeClass("big"); }, 1 );
});
div.small {
    background: #f00;
    width: calc((100vw - 4rem - 4rem) / 3);
    height: calc((100vh - 6rem) / 2);
    position: absolute;
    border-radius: 0.5rem;
    z-index: 6;
    top: 2rem;
    left: 2rem;
    transition: all .5s;
}

div.big {
    width: 100vw;
    height: 100vh;
    top: 0;
    margin-top: 0;
    left: 0;
    margin-left: 0;
    border-radius: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
<div class="small"></div>

【讨论】:

  • 烦人,但有一个简单的解决方法。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
相关资源
最近更新 更多