[英]Issue with CSS transitions in Chrome, Firefox and Opera, but not IE
In my web application, I want to animate switching between two elements. 在我的Web应用程序中,我希望动画化两个元素之间的切换。 The old element should disappear by sliding off to the left and fading out at the same time, after which the new element appears by sliding in from the right while fading in.
老元素应通过向左滑动并同时渐出而消失,此后,新元素通过渐入时从右侧滑动而出现。
Please see this fiddle: http://jsfiddle.net/mtKaz/8/ 请查看此小提琴: http : //jsfiddle.net/mtKaz/8/
I am trying to use CSS transitions for the animation. 我正在尝试对动画使用CSS过渡。 Everything works fine on first iteration, however once an element has been hidden once, the transition for the appearing element stops working: the appearing element just snaps in, without any motion or fading.
在第一次迭代中一切正常,但是一旦元素被隐藏一次,出现元素的过渡就停止工作:出现元素只是捕捉,没有任何运动或褪色。
I am experiencing this problem in Chrome, Opera Next and Firefox, but not in IE10. 我在Chrome,Opera Next和Firefox中遇到此问题,但在IE10中却没有 。 Am I doing something wrong or is this a bug in all browsers but IE (a most bizarre notion)?
我是在做错什么还是这是除IE(最奇怪的概念)以外的所有浏览器中的错误?
The issue seems to be related to show()
/ hide()
, since there is no problem if I set the elements to position: absolute
and remove the show()
/ hide()
calls. 这个问题似乎与
show()
/ hide()
,因为如果将元素设置为position: absolute
,则没有问题position: absolute
并删除show()
/ hide()
调用。
JavaScript: JavaScript:
var currentDiv = "div2";
$(function(){
$('#btn').click(function(){
var newDiv = currentDiv == 'div1' ? 'div2' : 'div1';
var oldDiv = currentDiv;
// Start transition for the disappearing div
$('#' + oldDiv).css({ opacity: 0, left: -100 });
// Prepare the new div for showing by first moving it to the right
// There will be a transition here as well, but it won't be visible
$('#' + newDiv).css({ opacity: 0, left: +100 });
setTimeout(function(){
// Once the transition is done, hide the div so it doesn't occupy space
$('#' + oldDiv).hide();
// Show the new div and immediately start its appearing transition
$('#' + newDiv).show().css({ opacity: 1, left: 0 });
}, 1000);
currentDiv = newDiv;
});
});
CSS: CSS:
.myDiv {
position:relative;
transition: left 1s, opacity 1s;
width: 200px;
}
HTML: HTML:
<div id="div1" class="myDiv" style="display:none">
hello, this is some text
</div>
<div id="div2" class="myDiv" style="display:none">
and here is some more text
</div>
There are issues with animating elements, which start with the attribute display: none. 动画元素存在一些问题,这些问题始于属性显示:无。 Wait for the complete callback of show() and then set the css properties.
等待show()的完整回调,然后设置css属性。
$('#' + newDiv).show(400, function(){
$(this).css({ opacity: 1, left: 0 });
});
See documentation for details: http://api.jquery.com/show/#show-duration-complete 有关详细信息,请参见文档: http : //api.jquery.com/show/#show-duration-complete
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.