我一直在尝试创建“卡”,这些卡在将其单击到窗口的整个视口时会展开。与在动画结束之前中断动画时的效果相去甚远,它会引起一些图形故障。我使用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。
最佳答案
这似乎确实是由使用相对单位触发的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>