gpt4 book ai didi

javascript - 中断 CSS 动画导致奇怪的转换

转载 作者:太空宇宙 更新时间:2023-11-04 00:43:57 26 4
gpt4 key购买 nike

我一直在尝试创建“卡片”,当您将它们单击到窗口的完整视口(viewport)时,这些卡片会展开。除了在动画完成之前中断动画之外,它一直运行良好,它会导致一些图形故障。我正在使用 CSS 转换而不是 Javascript 动画(我只是使用 JQuery 来制作所需类更改的原型(prototype))并且 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。

最佳答案

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

因此,您需要使用绝对单位来解决该问题。
在您的情况下,您可以简单地将所有初始 width:[..]%[..] 替换为 width:[..]vw[..] 和所有高度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>

关于javascript - 中断 CSS 动画导致奇怪的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57669912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com